0

I am writing asp.net project in C#.

I have a button in a page default.aspx, and I need javascript alert when I click the button and then update page. I do this by the following way:

protected void Button1_Click(object sender, EventArgs e)  
{  
Response.Write("<script language='javascript'>alert('OK');</script>");
Response.Redirect("default.aspx");
}

But javascript alert doesn't occur. So, how to make first appear javascript alert and then update page?

4 Answers 4

3

It is not working because you are calling the Response.Redirect. Anything that happens before this on the current page will be ineffective, because the new page will immediately redirect before the current page is rendered.

You have a couple of options, but the one I think you want is this...

protected void Button1_Click(object sender, EventArgs e)
{
  Response.Write("<script type='text/javascript'>");
  Response.Write("alert('OK');");
  Response.Write("document.location.href='default.aspx';");
  Response.Write("</script>");
}

The other option is to store the message you want to display in something like a session variable, and then show it on the new page after the redirection - but that is more complicated and requires the updating of the new page as well.

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

1 Comment

you are right, I've deleted my answer. The reason is Response.REdirect. VoteUP.
3

try this:

protected void Page_Load(....)
{
    this.myButton.Attributes.Add("onclick", "alert('OK'); return true;");
}

Or in the ASPX

<asp:Button runat="server" ID="myButton" onClientClick="alert('OK'); return true;" ... />

To execute the code after you have processed something on the server try the following:

ASPX

    <asp:ScriptManager runat="server" ID="scriptManager" />
    <asp:Button Text="text" runat="server" OnClick="dd_Click" />

Code behind

    protected void dd_Click(object sender, EventArgs e)
    {
        // add your cool stuff
        ScriptManager.RegisterStartupScript(this, typeof(RelatedUpdatePanels), "myKey", "alert('OK');window.location='newurl.aspx';", true);
    }

Comments

2

try following

 protected void Button1_Click(object sender, EventArgs e)  
    { 
      ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "<script   language='JavaScript'>alert('OK'); window.location.href = 'Default.aspx';</script>");  
    }

1 Comment

This cannot work, for exactly the same reason as the original question... doing anything to the current page before the redirect is completely ineffective
0

ClientScript.RegisterStartupScript(Page.GetType(), "validation", "alert('Invalid username or password'); document.location.href='Default.aspx';");

OR

ClientScript.RegisterStartupScript(Page.GetType(), "validation", "alert('Invalid username or password'); window.location.href='Default.aspx';");

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.