Navigate to a new page and display an alert box

怎甘沉沦 提交于 2019-12-20 04:25:03

问题


I am developing an application by using ASP.Net WebForm. Once user click a button, application will navigate to a new page and prompt out a dialog box "Welcome to JackiesGame"

However, I able to navigate to new page but the alert dialog box does not display.

The following is my sample code

void cmdCancel_Click(object sender, EventArgs e)
{
    HttpContext.Current.Response.Redirect(Globals.NavigateURL(TabId), true);
    Page page2 = HttpContext.Current.CurrentHandler as Page;
    ScriptManager.RegisterStartupScript(page2, page2.GetType(), "alertMessage", "alert('Insert Successfully')", true);
}

回答1:


Add the following in page 2. On the page load it will register only for the first time the page loads the script.

protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack)
   {
    var reg = Request["Welcome"]
       if(reg != null && reg.ToString() == "yes"){
          ScriptManager.RegisterStartupScript(this, this.GetType(), "alertMessage", "alert('Insert Successfully')", true);
      }
   }
}

All code after the redirect is getting ignored since it has to redirect to a new page. So the code never gets triggered.

EDIT Added a example of how it can look further

void cmdCancel_Click(object sender, EventArgs e)
{
    string myUrl = Globals.NavigateURL(TabId)+"?Welcome=yes";
    HttpContext.Current.Response.Redirect(myUrl, true);
}


来源:https://stackoverflow.com/questions/49644714/navigate-to-a-new-page-and-display-an-alert-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!