0


I have this javascript function :

function showLoader() {
           document.getElementById("loaderState").style.display = 'inline';
       }


And I have a button :

<asp:Button ID="btnSignUp" runat="server" Text="Sign Up" OnClick="btnSignUp_Click" />


I am tryong to call that js function in code behind :

protected void btnSignUp_Click(object sender, EventArgs e)
{
    try
    {
        int i = users.AddNewUser();
        if (i != 0)
        {
            Page.ClientScript.RegisterStartupScript(
                GetType(),
                "btnSignUp",
                "showLoader();",
                true);
        }
    }
    catch (Exception exp)
    {
        throw exp;
    }
}


But not worked!!! Why?
Thanks.

7
  • 1
    Have you tried to comment everything in btnSignUp_Click, then add just this line: Page.ClientScript.RegisterStartupScript(GetType(), "btnSignUp", "showLoader();", true); ? Maybe it's because the i value is 0 Commented Jan 18, 2014 at 13:12
  • No, i am sure that i is not 0 Commented Jan 18, 2014 at 13:16
  • I tested all code but if(i != 0) and it work fine. Commented Jan 18, 2014 at 18:45
  • possible duplicate of Execute javascript function after asp.net postback without Ajax Commented Jan 18, 2014 at 18:47
  • i was used update panel. if use onclientclick="showLoader()" it work fine, but if call it from code behind it does not work. Commented Jan 18, 2014 at 19:09

2 Answers 2

2

Use:

 ScriptManager.RegisterStartupScript(
 this,
 GetType(), 
"btnSignUp",
"showLoader();",
 true);

See these similar question:

ScriptManager.RegisterStartupScript code not working - why?

ClientScript.RegisterStartupScript not working

ScriptManager.RegisterStartUpScript(...) not working

Sign up to request clarification or add additional context in comments.

Comments

1

Try This

protected void btnSignUp_Click(object sender, EventArgs e)
{
    try
    {
        int i = users.AddNewUser();
        if (i != 0)
        {
ScriptManager.RegisterStartupScript(this, GetType(), "btnSignUp", "showLoader()", true);

        }
    }
    catch (Exception exp)
    {
        throw exp;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.