开发者

IsPostBack always returns false

开发者 https://www.devze.com 2023-03-18 13:43 出处:网络
I\'ve encountered a weird problem. Everytime I refresh the page, IsPostBack is false. To make sure nothing in my contents or master pages is causing this, I have created an empty web form and fired it

I've encountered a weird problem. Everytime I refresh the page, IsPostBack is false.

To make sure nothing in my contents or master pages is causing this, I have created an empty web form and fired it up in debug mode. still, 开发者_StackOverflow中文版on postbacks, I have IsPostBack set to false.

I have enableSessionState="true" and <sessionState timeout="30" /> in web.config.

It's driving me nuts!

Update: I refresh the page by hitting F5. Based on answers this should not cause a postback. I'd like to know when a use refreshes the page (even manually) and prevent some modifications to db).

Is there a solution for this?


Refreshing the page (pressing F5 or the refresh button in your browser) is not a postback. A postback occurs when a button is clicked, a dropdown is changed, or some other event on the page that causes data to be sent to the server (via HTTP POST, hence the name 'postback')

Your question doesn't make it clear whether you are refreshing the page manually or posting back to the server via a button click or some other event.

Since you are manually refreshing the page, IsPostBack will always be false.

There are two types of requests (in a sense) in ASP.NET:

  • a regular request (e.g. the user is loading the page for the first time)
  • a postback (e.g. a button was clicked on the page, sending data to the server)

If you want to track if a user has been to a page before in either case, you'll need to track that your self. You can set a variable in the Session to do this:

Session["UserHasVisitedThisPageBefore"] = true;

Then you can check it in place of your current IsPostBack check:

if(Session["UserHasVisitedThisPageBefore"] != null && (bool)Session["UserHasVisitedThisPageBefore"])
{
    // stuff here
}


When you refresh the page, the IsPostBack should be false. It becomes true when a control has caused a postback, such as a server side button.

Edit - To answer your update: You can use IsPostBack to determine whether or not you want to update the database. If it's false, don't update the database otherwise update it.

 if(IsPostBack)
 {
   //Update DB
 }


Sounds like you need some other mechanism to detect that the page was refreshed, like a counter. Viewstate, Session, hidden field - there are many options.


I had this problem of IsPostBack always being false in a particular project that had XSL rendering the markup. So instead of relying on .NET's IsPostBack property, I simply checked Request.HttpMethod.

e.g. if I needed to check !IsPostBack, I instead checked if Request.HttpMethod == "GET"

e.g. if I needed to check IsPostBack, I instead checked if Request.HttpMethod == "POST"


Every time you refresh page you basically perform GET request to you page. GET means, load the page without any change of state server. IsPostBack = false is absolutely right behaviour!

The postback would occur on page, if you do POST to it. Then the server side form is being submitted or web control with autoPostBack is triggered - it would cause a postback. IsPostBack will be true.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号