Search code examples
javascriptasp.net.netwebforms

Call JavaScript function (in aspx) on aspx.cs using a button


I have this aspx:

 <body>
    <div>
    <script type="text/javascript">
        function NewPage() {
            document.location.href = "http://www.nextservice.pt/"
        }
        </script>
         <form id="form1" runat="server">
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                    <asp:Button ID="Btn2" runat="server" Text="OK" onclick="Button2_Click" />

            CODE1: <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC0000" />
        </form>
        </div>
</body>

and I'm working with web forms, and I wont call this button on aspx.cs

   public partial class SITE_TESTER : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click (object sender, EventArgs e)
    {
        string code = TextBox1.Text.ToString();

        if (!verifyCode(code))  // comparing users from table 
        {
            Label1.Text = "Not Exists";  //for invalid code
        }
        else
        {
            Label1.Text = "Exist";  //for sucsseful code
            /*

            I Wont call my JavaScript Function here!!!!

            */
        }
    }
}

Solution

  • You can call a JavaScript method from server side in asp.net in the following ways:

    protected void button_Click(object sender , EventArgs e)
    {
            string jsMethodName = "NewPage()";
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "uniqueKey", jsMethodName, true);
    
          //or
          //ScriptManager.RegisterStartupScript(this, GetType(), "NewPage()", false); 
    }
    

    You can use either ScriptManager.RegisterStartupScript or ScriptManager.RegisterClientScriptBlock

    The difference between the two is explained below:

    Let's say we have a .aspx page with the following form tag: (Line nos. are for reference)

    1. <form id="Form1" runat="server">
    2. ..
    3. ..
    4. ..
    5. </form>
    

    Now let's look at key differences for each method :

    A. Page.RegisterClientScriptBlock() will insert the block of script before Line 2. Page.RegisterStartupScript() will insert the script after Line 4.

    B. Page.RegisterClientScriptBlock() should usually be used for scripts encapsulated in functions. (hence the word "block") Page.RegisterStartupScript() can be used for any script, even if it's not in a function.

    C. Page.RegisterClientScriptBlock() should be used for functions that don't need to run on Page load. Page.RegisterStartupScript() should be used for scripts that must run on Page Load.

    D. Page.RegisterClientScriptBlock() should be used for a script that does not require the form elements to have been created. Page.RegisterStartupScript() should be used for scripts that require the form elements to have been created and uses references to them.

    Notice that all the 4 differences are essentially related to each other (they build upon the prev. one). The difference put in one line can sometimes be too subtle.

    You can learn more about these from here and here