) IsPostBack usage and ispostback usage

Source: Internet
Author: User

(Conversion) IsPostBack usage and ispostback usage

On the Internet one day, someone wrote the following code:

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
DropDownList1.AutoPostBack = true;
}
ArrayList address = new ArrayList ();
Address. Add ("us ");
Address. Add ("China ");
Address. Add ("Japan ");
DropDownList1.DataSource = address;
DropDownList1.DataBind ();
}
Protected void dropdownlistincluselectedindexchanged (object sender, EventArgs e)
{
Switch (DropDownList1.SelectedValue)
{
Case "China ":
{
ArrayList china_address = new ArrayList ();
China_address.Add ("Hunan ");
China_address.Add ("Guangdong ");
China_address.Add ("Guangxi ");
DropDownList2.DataSource = china_address;
DropDownList2.DataBind ();
Break;
}
Case "USA ":
{
ArrayList american_address = new ArrayList ();
American_address.Add ("Los Angeles ");
American_address.Add ("San Andreas ");
DropDownList2.DataSource = american_address;
DropDownList2.DataBind ();
Break;
}
Case "Japan ":
{
ArrayList japan_address = new ArrayList ();
Japan_address.Add ("Kobe ");
Japan_address.Add ("dashboard ");
DropDownList2.DataSource = japan_address;
DropDownList2.DataBind ();
Break;
}
}
}

Then there is a problem.

The result of opening the page for the first time is as follows:

 

 

Select China. The result is as follows:

 

 

What about Japan? The result is still:

 

 

The question is that the person who asked the question did not understand the concept of IsPostBack, which may be a concept that many beginners can easily confuse. You only need to bind the data in the drop-down menu when loading the page program for the first time. The example above is written in If (! IsPostBack) outside the code block. In this way, the data source will be re-bound every time the server responds to the client's sending back, so the above error will occur.

Check msdn, which has the IsPostBack definition above: Get a value indicating whether the page is being loaded in response to the client's sending back, or whether it is being loaded and accessed for the first time. If the page is loaded in response to the client sending back, it is true; otherwise, it is false.

"Load for the corresponding client sending back", note that the client sending back here refers to the server responding to the client sending back, this determines that this attribute is caused by the event of the server control. For javascript client script code, execution of this attribute cannot be triggered because it is only running on the client.

Therefore, in the preceding example, the code is as follows:

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
DropDownList1.AutoPostBack = true;
}
ArrayList address = new ArrayList ();
Address. Add ("us ");
Address. Add ("China ");
Address. Add ("Japan ");
DropDownList1.DataSource = address;
DropDownList1.DataBind ();
}
Changed:

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
DropDownList1.AutoPostBack = true;

ArrayList address = new ArrayList ();
Address. Add ("us ");
Address. Add ("China ");
Address. Add ("Japan ");
DropDownList1.DataSource = address;
DropDownList1.DataBind ();
}

}
OK. The result is what we want. The specific result is as follows:

Result:

 

 

After selecting Japan, the result is as follows:

 

 

After selecting the United States, the result is:

 

 

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.