It is very important to check for IsPostBack during Page_Load.

Why this is so is because, weird things might happen otherwise.

In the Movies CMS I was working on, there is a page where the administrator adds a person, either as a Actor, Actress, or Director. I've entered the name, and I clicked on Director. In my button event code however, when I take the value of ddlRoles.SelectedItem.Value, it is not the expected value! I selected the third item in the DropDownList, yet I keep getting the first item as the selection when I read it from the DropDownList.

I keep scratching my head, and wondered why. Finally, I figured out that the bug is due to the Page_Load code being called, even when I'm pressing a button. When I pressed the button, the Page_Load code gets called before the button event code gets called. Needless to say, I've fixed the code.

Here's my “before” Page_Load code.

daPeople.Fill(dsPeople1, "People");
daRoles.Fill(dsRoles1, "Roles");
daPeopleRole.Fill(dsPeopleRole1, "PeopleRoles");
dgPeopleRole.DataBind();
ddlRoles.DataBind();

And here's my “after” Page_Load code

If (!IsPostBack)
{
daPeople.Fill(dsPeople1, "People");
daRoles.Fill(dsRoles1, "Roles");
daPeopleRole.Fill(dsPeopleRole1, "PeopleRoles");
dgPeopleRole.DataBind();
ddlRoles.DataBind();
}