How do I refresh the page in ASP.NET? (Let it reload itself by code)

后端 未结 14 1845
悲&欢浪女
悲&欢浪女 2020-11-28 02:12

How do I refresh a page in ASP.NET? (Let it reload itself by code)

I\'d rather not use Response.Redirect() because I don\'t know if the page I will be on, as it\'s i

相关标签:
14条回答
  • 2020-11-28 02:41

    In your page_load, add this:

    Response.CacheControl = "no-cache";
    Response.AddHeader("Pragma", "no-cache");
    Response.Expires = -1;
    
    0 讨论(0)
  • 2020-11-28 02:43

    I personally need to ensure the page keeps state, so all the text boxes and other input fields retain their values. by doing meta refresh it's like a new post, IsPostBack is always false so all your controls are in the initialized state again. To retain state put this at the end of your Page_Load(). create a hidden button on the page with an event hooked up, something like butRefresh with event butRefresh_Click(...). This code sets a timer on the page to fire a postback just like a user clicked the refresh button themselves. all state and session is retained. Enjoy! (P.S. you may need to put the directive in the @Page header EnableEventValidation="false" if you receive an error on postback.

    //tell the browser to post back again in 5 seconds while keeping state of all controls
    ClientScript.RegisterClientScriptBlock(this.GetType(), "refresh", "<script>setTimeout(function(){ " + ClientScript.GetPostBackClientHyperlink(butRefresh, "refresh") + " },5000);</script>");
    
    0 讨论(0)
  • 2020-11-28 02:43
    Response.Write("<script>window.opener.location.href = window.opener.location.href </script>");
    
    0 讨论(0)
  • 2020-11-28 02:44

    Try this:

    Response.Redirect(Request.Url.AbsoluteUri);
    
    0 讨论(0)
  • 2020-11-28 02:44

    There are various method to refresh the page in asp.net like...

    Java Script

     function reloadPage()
     {
         window.location.reload()
     }
    

    Code Behind

    Response.Redirect(Request.RawUrl)
    

    Meta Tag

    <meta http-equiv="refresh" content="600"></meta>
    

    Page Redirection

    Response.Redirect("~/default.aspx"); // Or whatever your page url
    
    0 讨论(0)
  • 2020-11-28 02:47

    This might be late, but I hope it helps someone who is looking for the answer.

    You can use the following line to do that:

    Server.TransferRequest(Request.Url.AbsolutePath, false);
    

    Try to avoid using Response.Redirect as it increases the steps in the process. What it actually does is:

    1. Sends back the page with redirection header
    2. The Browser redirects to the destination URL.

    As you can see, the same outcome takes 2 trips rather than 1 trip.

    0 讨论(0)
提交回复
热议问题