ASP. NET IsPostBack Attribute Analysis

Source: Internet
Author: User

ASP is a Microsoft technology. It is a Server-side scripting technology that enables scripts embedded in web Pages to be executed by Internet servers. It refers to Active Server Pages (dynamic Server Pages ), programs Running in IIS.


Learning ASP. NET Video, first understand that the first contact is the form loading event. When we request the server for the first time in the browser, Page_Load will be executed, and Page_Load will be executed when it is not the first request. Why do I have multiple requests for the same Form? Asp.net introduces server-side events. controls that support server-side events will send requests to the current Form. In this way, in many cases, we need to determine whether it is the first request to this Form. To solve this problem, we introduce the IsPostBack attribute of PostBack:

 


Before going deep into this attribute, we should first distinguish the difference between PostBack IsPostBack: the basic knowledge of the aspx page return mechanism knows that postback is a return, that is, when the page is loaded for the first time, it submits data to the server, the server then passes the processed data to the client and displays it. This is called postback. ispostback is only a property, that is, to determine whether the page is returned. if (! Ispostback) indicates that the page is loaded for the first time. This is a common judgment method. A page can only be loaded once, but it can be repeatedly postback after loading.

 

IsPostBack is an attribute of the bool type in the Page class. It is used to determine whether the current Form request is the first or not the first request. When IsPostBack = true, it indicates not the first request, which is called PostBack. When IsPostBack = false, it indicates the first request. There are many scenarios in the asp.net framework that need to judge IsPostBack. For example, LoadAllState and other operations need to be performed during PostBack. When we use WebForm for development, we often judge IsPostBack in Page_Load, because Page_Load is executed in the first request and Page_Load is executed in non-first request. Why do I have multiple requests for the same Form? Asp.net introduces server-side events. controls that support server-side events will send requests to the current Form. In this way, in many cases, we need to determine whether this Form is the first request.

 

 

 

Code parsing:
1. IsPostBack = false of the page migrated when Server. Transfer is used for Migration. Every time the page is refreshed, the page is loaded for the first time;

2. if there is no Request value in the Post method Request, that is, Request. if Form = null, IsPostBack = false; if the Get method does not contain a Request value, that is, Request. if QueryString = null, IsPostBack = false.

 

[Csharp]
<SPAN style = "FONT-FAMILY: SimSun; FONT-SIZE: 18px"> namespace Example
{
Public partial class WebForm1: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack) // The value uploaded from the page is blank when the page is loaded for the first time.
{
If (Request. QueryString ["id"]! = Null)
{
Response. Write ("return ");
}
 
Else
 
{
 
Response. Write ("not a callback ");
 
}
}

}
 

 
Protected void button#click (object sender, EventArgs e)
{
Response. Redirect ("WebForm1.aspx? Id = 3 ");
}
}
} </SPAN>

Namespace Example
{
Public partial class WebForm1: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack) // The value uploaded from the page is blank when the page is loaded for the first time.
{
If (Request. QueryString ["id"]! = Null)
{
Response. Write ("return ");
}

Else

{

Response. Write ("not a callback ");

}
}

}

 

Protected void button#click (object sender, EventArgs e)
{
Response. Redirect ("WebForm1.aspx? Id = 3 ");
}
}
}

 

 

3. IsPostBack = false

 


4. Cross-Page Submission (CrossPagePostBack) occurs. When you access the PreviousPage attribute, IsPostBack = true for The Source Page. When cross-Page Submission (CrossPagePostBack) occurs, the target page is IsPostBack = false.

 


5. Use Server. Execute to migrate the page to its IsPostBack = false.

 

 

 

Differences between Server. Execute and Server. Transfer

Server. Execute ("another. aspx") and Server. Transfer ("another. aspx") are different:
Execute transfers the execution from the current page to the specified page and returns the execution to the current page.
Transfer completely transfers the execution to the specified page

 

 

 

Instance 1:
[Csharp
<SPAN style = "FONT-FAMILY: SimSun; FONT-SIZE: 18px"> protected void Page_Load (object sender, EventArgs e)
{
If (! This. IsPostBack)
{
SqlConnection con = DB. createConnection ();
Con. Open ();
SqlCommand cmd = new SqlCommand ("select * from personLike", con );
SqlDataReader sdr = cmd. ExecuteReader ();
This. CheckBoxList1.DataTextField = "likeContent ";
This. CheckBoxList1.DataValueField = "id ";
This. CheckBoxList1.DataSource = sdr; </SPAN>

Protected void Page_Load (object sender, EventArgs e)
{
If (! This. IsPostBack)
{
SqlConnection con = DB. createConnection ();
Con. Open ();
SqlCommand cmd = new SqlCommand ("select * from personLike", con );
SqlDataReader sdr = cmd. ExecuteReader ();
This. CheckBoxList1.DataTextField = "likeContent ";
This. CheckBoxList1.DataValueField = "id ";
This. CheckBoxList1.DataSource = sdr;


Assume that textbox is defined as null in page_load (), and write a segment of textbox value to the data inventory in the button_click event. after you enter a value in the textbox and click the button, you can save the value entered in the textbox to the database. in fact, when you click a button, the system will automatically re-Execute page_load (), that is, clear the textbox of the handle, and then execute the click event, the value you save to the database is always empty. therefore, add if (! IsPostBack ).

 

 

 

2. solve the problem that the data updated during data editing cannot be modified.

Resolution: When editing data in the background, the system first queries the data to be edited from the background. In this case, the data is queried from the database without returning the data. After editing the data, click the submit button to submit the data. If you do not determine whether to return the data, the original data will be queried again, resulting in no change in the editing data.

 

 

[Csharp]
<SPAN style = "FONT-FAMILY: SimSun; FONT-SIZE: 18px"> namespace Example
{
Public partial class WebForm1: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack) // The value uploaded from the page is blank when the page is loaded for the first time.
{
 

 
}
If (Request. QueryString ["id"]! = Null)
{
Bind ();
}
Else
{
Response. Write ("<script> alert ('data changed ') </script> ");
}

}
Public void Bind ()
{
Response. Write ("data ");
}
Protected void button#click (object sender, EventArgs e)
{
Response. Redirect ("WebForm1.aspx? Id = 3 ");
// Server. Execute ("WebForm2.aspx? Id = 3 ");
}
 

 
Protected void CesH_Click (object sender, EventArgs e)
{
Label1.Text = "test ";
}
}
} </SPAN>

Namespace Example
{
Public partial class WebForm1: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack) // The value uploaded from the page is blank when the page is loaded for the first time.
{

 

}
If (Request. QueryString ["id"]! = Null)
{
Bind ();
}
Else
{
Response. Write ("<script> alert ('data changed ') </script> ");
}

}
Public void Bind ()
{
Response. Write ("data ");
}
Protected void button#click (object sender, EventArgs e)
{
Response. Redirect ("WebForm1.aspx? Id = 3 ");
// Server. Execute ("WebForm2.aspx? Id = 3 ");
}

 

Protected void CesH_Click (object sender, EventArgs e)
{
Label1.Text = "test ";
}
}
}

 

 

IsPostBack conclusion
  
① IsPostBack = false for the page to be migrated when Server. Transfer is used for migration.
  
② In Post mode, if there is no Request value in the Request, that is, Request. if Form = null, IsPostBack = false; if the Get method does not contain a Request value, that is, Request. if QueryString = null, IsPostBack = false.
  
③ If QueryString or Form has a request value, but the Key in QueryString or Form does not have "_ VIEWSTATE", "_ EVENTTARGET", and "_ VIEWSTATEFIELDCOUNT ", if no key is "null" and the value starts with "_ VIEWSTATE" and the value is "_ EVENTTARGET", IsPostBack = false.
  
④ When you use Response. Redirect to migrate to the image, IsPostBack = false.
  
⑤ Cross-Page Submission (CrossPagePostBack) occurs. When the PreviousPage attribute is accessed, IsPostBack = true for The Source Page.
  
⑥ When cross-Page Submission (CrossPagePostBack) occurs, the target page is IsPostBack = false
  
7. Use Server. Execute to migrate the page to its IsPostBack = false.
  
When a volume is running on a Page, its corresponding DLL is updated and the Page tree structure has changed. In this case, IsPostBack = false.

  
We can understand these conclusions as follows: IsPostBack = false is generally used to determine whether the Request contains no Request value. If a request value does not include some special keys or values such as "_ VIEWSTATE", IsPostBack = false (after each request. net Framework will return some special hidden domain "_ VIEWSTATE" to the client ). There are also some special situations where the above rules cannot be correctly judged and need special processing, including Server. transfer, Response. redirect, CrossPagePostBack, Server. execute, page element changes and re-compilation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.