How to get alert message before redirect a page

前端 未结 7 1197
面向向阳花
面向向阳花 2020-12-25 07:49

I\'m using vs 2010. I need to display the message to user and redirect the page.

I use the below line.

ScriptManager.RegisterStartupScript(this, this         


        
相关标签:
7条回答
  • 2020-12-25 08:30

    Your Asking for a redirect page, below is an example of a redirect:

       string page = "Login.aspx";
            string myStringVariable = "Password Update!";
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');Response.Redirect('"+ page +"' );", true);
    
    0 讨论(0)
  • 2020-12-25 08:40

    If you want to put in .CS file, just try this:

    var page = HttpContext.Current.CurrentHandler as Page;
               ScriptManager.RegisterStartupScript(page, page.GetType(), "alert", "alert('" + msg +"');window.location ='"+ aspx +"';", true);
    
    0 讨论(0)
  • 2020-12-25 08:42

    If you are Working with UpdatePanel then You have to use this : It is work with Update Panel.

     var message = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize("Bill No. : " + BillNo + " successfully generated.");
                var script = string.Format("alert({0});window.location ='ChhallanPrint.aspx';", message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", script, true);
    
    0 讨论(0)
  • 2020-12-25 08:45

    You need to write:

     ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('User details saved sucessfully'); window.open('frmDisplayUsers.aspx');", true);
    

    Please note that I have removed the script tags as the last parameter true means you must not use the script tag.

    This code worked for me. If you have any problem let me know. In addition you can use setTimeout to delay the window open that might not be a very bad choice.

    0 讨论(0)
  • 2020-12-25 08:48

    The best way, if possible, is to put the code into the OnClientClick.

    <asp:Button OnClientClick=" alert('blah?');" runat="server" />
    

    The problem with putting it into a startupscript is that the startupscript is run on postback, not real time. The redirect will happen before the postback. (possibly)

    The other solution of course is to put the redirect into the startupscript code.

    The page will post back, the script will run and then it will redirect.

    0 讨论(0)
  • 2020-12-25 08:51

    Your code is opening window but your asking for a redirect, below is an example of a redirect:

    ScriptManager.RegisterStartupScript(this, this.GetType(), 
    "alert", 
    "alert('User details saved sucessfully');window.location ='frmDisplayUsers.aspx';", 
    true);
    
    0 讨论(0)
提交回复
热议问题