1

What I'm trying to do is, when I click on the button it should show an alert box. Here is the code.

protected void btnAdd_Click(object sender, EventArgs e)
{
    string script = "<script type=\"text/javascript\">alert('abc 1');
    </script>";
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert 1", 
    script);
    Response.Redirect("~/Index.aspx");

}

It is working perfectly but when I put this same code in another page it does not work.

what are the things I have to look into? any suggestion please?

5
  • 1
    In what way does it not work? Are errors reported in the developer console? Does anything happen? Commented Nov 13, 2017 at 14:47
  • no nothing, just redirects to Index.aspx page Commented Nov 13, 2017 at 14:52
  • Of course it does. The code is not gonna render and then wait for that alert. Your script will never reach the browser. Commented Nov 13, 2017 at 15:02
  • @VDWWD thank you very much. So what I have to do then? Commented Nov 13, 2017 at 15:17
  • @user2983359 depends on what you are trying to do. But the answer from KB1788 looks good. Commented Nov 13, 2017 at 15:38

3 Answers 3

1

move your js code to client side

<asp:Button ID="btnAdd" OnClientClick="alert('alert');" OnClick="btnAdd_Click" runat="server"/>

protected void btnAdd_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Index.aspx");

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

3 Comments

but on the client side I have already one function. I want to show a dialog box to the user that your pw has been changed and that's it.
I see, then you can either have redirect on client side: protected void btnAdd_Click(object sender, EventArgs e) { string script = "<script type=\"text/javascript\">alert('abc 1');window.location.href="Index.aspx"; </script>"; ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert 1", script); } or just redirect to Index.aspx with some param (e.g. index.aspx?showConfirmation=true) and show confirmation there
if I remove Response.Redirect("~/Login.aspx"); it works and it shows the dialogue box. What could be reason? but it stays on the same page.
1

May be you can try this - syntax may be wrong -

  protected void btnAdd_Click(object sender, EventArgs e)
{ 
    Response.Redirect("~/Index.aspx?Issuccess=1");

}

index.aspx page - 

protected void Page_Load()
{
     if(Request.QueryString["Issuccess"] != null && Convert.ToInt32(Request.QueryString["Issuccess"]) == 1)
     {
            string script = "<script type=\"text/javascript\">alert('abc 1');
            </script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert 1", 
            script);
     }
}

5 Comments

I have tried your solution but no luck. I'm sorry brother.
@user2983359 Define "doesn't work". Do you get an error in the JavaScript console? Is the code in Page_Load reached? Does it redirect with the proper query string? You need to be a lot more specific, don't just throw your hands in the air and say it doesn't work because that's not helpful at all.
@mason it just redirects to the index.aspx page. It does not execute the javascript code. I don't get any message in javascript console.
@user2983359 Does it redirect with the proper query string? Does it enter the page load method? What happens when you step through the code line by line in the debugger? Again, you need to be far more specific than what you're being. When you debug code, it's your responsibility to check each piece to make sure it's doing what you think it should do.
if I remove Response.Redirect("~/Login.aspx"); then the javaScript code runs.
0

Pass the last Parameter either True or False in ClientScript.RegisterScript and better you pass the alert message in this way.

Like This

 protected void btnAdd_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('abc 1');",true);
        Response.Redirect("~/Index.aspx");
    }

3 Comments

I have done exactly as you said but no luck brother. I don't know what is wrong :(
Or Else Use this method
Before Using this, Please check the namespace using System.Web.UI is added or not. ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('The Username and Password was incorrect')", true);

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.