pepe
pepe

Reputation: 149

Where is stored the property IsPostBack? in asp.net

I have this program in asp.net

<body>
    <form id="form1" runat="server">
      <div>    
         <asp:Button runat ="server" ID="btnTest" Text ="Request Somethig"   
         OnClick ="OnClick" />
       </div>
    </form>
</body>

And the code behind:

public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (IsPostBack)
    Response.Write("A Post Back has been sent from server");
   }

protected void OnClick(object sender, EventArgs e)
{          
   //The button has AutoPostBack by default
 }  

}

If I request the page to the server http://localhost:50078/Default.aspx ,the server will create an instance of the class _Default.cs, then it will fires and event Page_Load, and this line won't be executed the first time:

Response.Write("A Post Back has been sent from server");

And the reason is that IsPostBack=false

Then if I hit click on the button, I will request a post back from server, so now IsPostBack will be true and in my browser I will see the message

"A Post Back has been sent from server"

My question is: How the property IsPostBack is changed from false to true, and where is storage that value?

As far as I know, the instance that the server creates from the class _Default.cs is destroyed once the HTML is sent to the client,so, it suppose to have nothing about IsPostBack property when I click the button(doing a post back).

Does the server storage the value of IsPostback in a _VIEWSTATE hidden variable in the page itself?

Thanks in advance!!

Upvotes: 0

Views: 691

Answers (1)

devlin carnate
devlin carnate

Reputation: 8591

IsPostBack is a public property of the Page class. Daryal's answer to this question explains the structure of that class.

From that answer:

Page class derives from TemplateControl class;

public class Page : TemplateControl, IHttpHandler

and TemplateControl class derives from abstract Control class;

public abstract class TemplateControl : Control, ...

In Control class which Page class derives from there is a virtual property named as Page;

// Summary:
//     Gets a reference to the System.Web.UI.Page instance that contains the server
//     control.
//
public virtual Page Page { get; set; }

In the Page class there are properties like IsPostBack, IsValid etc;

// Summary:
//     Gets a value that indicates whether the page is being rendered for the first
//     time or is being loaded in response to a postback.
//        
public bool IsPostBack { get; }

Upvotes: 2

Related Questions