Sunday, 30 May 2021

How to get alert message before redirect a page in ASP.Net

Step 1: - Set addScriptTags: false, if you used <script> tag in your script

protected void Button1_Click(object sender, EventArgs e)
{
    string scriptText = "<script>alert('Hi');window.location.href='https://www.google.com'</script>";
 
    ScriptManager.RegisterStartupScript(control: Page, type: typeof(Page), key: "AlertBox", script: scriptText, addScriptTags: false);

}

Step 2: - Set addScriptTags: true, if you are not used <script> tag in your script

protected void Button1_Click(object sender, EventArgs e)
{
    string scriptText = "alert('Hi');window.location.href='https://www.google.com'";
 
    ScriptManager.RegisterStartupScript(control: Page, type: typeof(Page), key: "AlertBox", script: scriptText, addScriptTags: true);

}

Note: -

1.  If your button, not in the update panel & not using the update panel then alert show on the blank page & then redirect to next page

2.  You can also use below code-

ScriptManager.RegisterClientScriptBlock(control: Page, type: typeof(Page), key: "AlertBox", script: scriptText, addScriptTags: true);






 

 

No comments:

Post a Comment