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.

Farzaneh Talebi
  • 835
  • 4
  • 22
  • 47
  • 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 – ekad Jan 18 '14 at 13:12
  • No, i am sure that i is not 0 – Farzaneh Talebi Jan 18 '14 at 13:16
  • I tested all code but `if(i != 0)` and it work fine. – Amir Jan 18 '14 at 18:45
  • possible duplicate of [Execute javascript function after asp.net postback without Ajax](http://stackoverflow.com/questions/320999/execute-javascript-function-after-asp-net-postback-without-ajax) – Amir Jan 18 '14 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. – Farzaneh Talebi Jan 18 '14 at 19:09
  • @farzaneh_t: first: you don't explain about `UpdatePanel`. second: i tested your codes without `UpdatePanel`. – Amir Jan 18 '14 at 19:25
  • Sorry, I realized why my code does not work. I had used Response.Redirect after calling the JavaScript function. – Farzaneh Talebi Jan 19 '14 at 13:36

2 Answers2

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

Community
  • 1
  • 1
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;
    }
}
Somnath
  • 249
  • 3
  • 12