Shuan-han Chen Shuan-han Chen - 1 month ago 20
ASP.NET (C#) Question

Show message and Reload page in C# asp.net webform

I want to show message and reload page at same time,

But it has been kept reload page and can not stop.

How can I fix this problem? thanks.

protected void btnCk_Click(object sender, EventArgs e)
{
string a= Request.Form["a"];
string b= Request.Form["b"];
string c= Request.Form["c"];

string strReturn = null;

strReturn = U9.Data.LoginData.LoginChange(a, b, c);

if (strReturn == "0")
{
Response.Write("<Script language='JavaScript'>alert('OK');</Script>");
}
else
{
Response.Write("<Script language='JavaScript'>alert('"+ strReturn.ToString() + "');</Script>");
}

Response.Write("<Script language='JavaScript'>window.location.reload();</Script>");
Response.End();
}

Div Div
Answer

I think this issue is because of you are you're trying with Response.Write which writes a string to an HTTP response output stream.

In this case, you can use ClientScriptManager.RegisterStartupScript Method

if (strReturn == "0")
{
    ClientScript.RegisterStartupScript(Page.GetType(), "alert", "alert('OK');window.location='default.aspx';", true);
}
else
{
    ClientScript.RegisterStartupScript(Page.GetType(), "alert", "alert('"+ strReturn.ToString() + "');window.location='default.aspx';", true);
}

And change your page at the place of default.aspx

Hope this helps!