简体   繁体   中英

How to embed following href javascript code to a button in asp.net

I am creating a asp.net website. It has a file upload button. Uploading href works fine with following code. But i want to embed this code in a button.

<a href="javascript:$('#<%=FileUpload1.ClientID%>').fileUploadStart()">Start Upload</a>

here is the button that i want to embed above javascript code. and also i want the click event to this button.

<input type="button"  value="upload" runat="server"  name="btnBeforeOk" onserverclick="btnBeforeOk_ServerClick" />

This is my c# code for the above button

        protected void btnBeforeOk_ServerClick(object sender, EventArgs e)
    {
        //Response.Write("<script type=\"text / javascript\">window.location.href = \"javascript:$('#<%=FileUpload1.ClientID%>').fileUploadStart()\"; </ script > ");
        //Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "onlick();", true);
        ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "onlick()", true);
    }

I'm calling following javascript function from that button's C# code

<script type="text/javascript">
         function onlick() {
    location.href = "javascript:$('#<%=FileUpload1.ClientID%>').fileUploadStart()"
    alert("Alert works well but above code is not running")
}
            </script>

Alert part of this code is working well. but that small href code above the alert is not running well as it is working well with '<a href>' tag in asp.net. I also tried to run that small code with "window.location" and "windows.location.href". but it is not working. i was searching on google for 2 days and did many solutions. nothing worked.

That line of code isn't working because all you're doing is setting location.href, equal to the value of a string literal (the idea of setting window.loaction.href, to a string with a value of a valid url is the right course of action), however the string literal in this case is not aa valid url.

It works inline in your html, because the string is interpreted as javascript, it executes the line of code, and that line of code produces a string with a value of the url that you want. However in this case, that line of code is already in a script, and is interpreted so.

Therefore in order for the it to be executed, you need it to not be a string literal, as it currently is, but rather be it's own line of actual code.

Like this:

<script type="text/javascript">
    function onlick() {
        window.location.href = $('#<%=FileUpload1.ClientID%>').fileUploadStart()
    }
</script>

This will execute the "fileUploadStart" function on the jquery object returned by your jquery selector, ie the "$('#<%=FileUpload1.ClientID%>')" part of your code.

Which should presumably return the url you're after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM