IsPostBack


You might have come accross some scenarios like the one below : 

Scenario : "Why doesn't my DropDownList keep it's Selection after user selects an item from it and clicks a button?"

Scenario:

Say you put a DropDownList on your web page. Then, at Page_Load event of your code-behind cs file , you either populate the list items manually, or bind it to a database table so that the items are available for user to make a choice when the page loads. When you bind a dropdownlist or other databound controls to a datasource during the page_load event, the process is triggered each time the page is loaded.

In the following code, we call a method which will populate a dropdownlist on the Page_Load Event:-
private void Page_Load(object sender, System.EventArgs e)
{
 BindDropDownList1();
}
Now at some point, when the end user makes a choice from the dropdownlist, he/she gets more data in return.

Now when user selects an item from list and clicks some button, you write a code for changes to occur in ButtonClick Event of code-behind cs file.

Now, when that event gets fired? Its after a Page_Load event again. Try putting breakpoints in Page_Load event and see the sequence of event firing. Every time the Page load or postbacks, the Page_Load event gets fired. So, the dropdownlist wil again get populated and initially selected index by the user will be lost. So, You need to check at Page_Load event, whether the Page is posted back by some button click or by some index change in List controls.

To avoid this, we must put the dropdownlist populating code within the IsPostBack condition, as follows:-
private void Page_Load(object sender, System.EventArgs e)
{
 if(!IsPostBack)
 {
  BindDropDownList1();
 }
}
This would ensure that the dropdownlist is not repopulated during each postback and if you try to get the selected item, you will get the correct selected item even though the page postbacks.