How to send an alert and also refresh the page?

2019-08-06 01:48发布

I've been reading some topics from StackOverFlow and I found some ways to refresh the page like:
Response.Redirect(Request.RawURL);, Response.Redirect(Absolute.Uri) etc...

But I need to send an alert of confirmation and ALSO refreshes the page... Now, I'm using this method to send the alert:

ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('Well Done !');", true);  

But using any of these methods to refreshes the page, the alert isn't fired. So I need to know a method that is a good practice to do this.
Because I have some DropDownList with data from database and I'm making some Edits on the data, so when the user clicks on the button to edit the current data, it shows up a message like edit successfully then refreshes the page with the new data.

标签: asp.net
2条回答
Emotional °昔
2楼-- · 2019-08-06 02:30

How about client redirect?

alert("You're now will be redirected");
window.location.reload();

Since alert() is synchronous function, redirect will fire only when user click "ok"

查看更多
Bombasti
3楼-- · 2019-08-06 02:41

The above client script can be added in your codebehind this way.

protected void Render()
{
    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder("<script "
        + "type='text/javascript'>"
        );
    stringBuilder.Append("alert('hi');");
    stringBuilder.Append("window.location.reload();");
    stringBuilder.Append("</script>");

}
查看更多
登录 后发表回答