Hi sambath,
You can display alert box before redirection. Refer below article.
But you can't show alert message after redirect as you are doing.
For displaying alert message after redirection you may use Session or QueryString and on page Load check if the Session or QueryString is not empty then display alert message.
Check the example.
HTML
<asp:Button ID="btnRedirectOther" runat="server" Text="Redirect To Other" OnClick="btnRedirectOther_Click" />
<asp:Button ID="btnRedirectCurrent" runat="server" Text="Redirect To Current" OnClick="btnRedirectCurrent_Click" />
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["msg"]) && Request.QueryString["msg"] == "Success")
{
string message = "The Record is sucessfully added";
string script = "window.onload = function(){";
script += "alert('" + message + "');";
script += "}";
ClientScript.RegisterStartupScript(this.GetType(), "Message", script, true);
}
}
protected void btnRedirectCurrent_Click(object sender, EventArgs e)
{
string url = Request.Url.AbsoluteUri + "?msg=Success";
string script = "window.onload = function(){";
script += "window.location = '" + url;
script += "';}";
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}
protected void btnRedirectOther_Click(object sender, EventArgs e)
{
string message = "update sucessful";
string url = "search.aspx";
string script = "window.onload = function(){ alert('";
script += message;
script += "');";
script += "window.location = '";
script += url;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}
Screenshot
