Monday, March 28, 2011

Why IsPostBack Check?

Binding Data
We often bind data to Dropdown lists and Repeater etc, in ‘PageLoad’ event. Sometimes we bind data without checking page ‘IsPostBack’ value.Due to this small mistake, the changes that we made to dropdown selection will be lost. When trying to catch the selected value of the dropdown list in the ‘Post back’ event, it will be set to default value. Any novice learner of .Net will get surprised to see, if their changes were not effecting.
Here what happens is the ’Page Load’ events occurs first, and then the ‘Post back’ event. Here the drop down list (in our case) binds two times. Whenever we submit a page by clicking on the button, the first event that takes place is ‘Page Load’ event then the ‘Button click’ event. So the BindDropDown() will executes again and the changes that we made will get lost.
So here the conclusion is Data Binding operation should be done before page post back.
Here is the Sample Code:Binding the data by checking the page ‘Post Back’ event.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Page Post back and Databinding</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<br />
<asp:DropDownList ID="ddlBooks" runat="server" EnableViewState="true" ></asp:DropDownList>
<asp:Button ID="btnSubmit" OnClick="BtnSubmitClicked" Text="Submit" runat="server" />
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.ObjectModel;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(objectsender, EventArgs e)
{
if (!IsPostBack)
{
BindDropDown();
}
}
private void BindDropDown()
{
List<string> books = new List<string>();
books.Add("C# fundamentals");
books.Add("Masteringin Styles");
books.Add("C#Advanced");
books.Sort();
ddlBooks.DataSource = books;
ddlBooks.DataBind();
ddlBooks.Items.Insert(0,new ListItem("Select ne","select"));
}
protected void BtnSubmitClicked(object sender, entArgs e)
{
lblStatus.Text =ddlBooks.SelectedValue;
}
}
Try to remove the IsPostBak condition and run the code.see the difference.

Another mistake we often do is forget to ‘Enable View State’ of the page. By default view state is enabled for all controls, there is no need to explicitly enable it. However, if we disable it in ‘web.config’ file, we have to explicitly enable it where ever we want. Here are some guide lines to enable view state of drop down list.

‘View State’ can be disabled/enabled in
I. Web.config file (to disable ‘view sate’ of all pages)
II. Master page
III. Aspx Page.
IV. User control
V. With in the Control itself.
Take an example of ‘drop down list’, when the view state set to false for all pages in ‘web.config’ file,
view state won’t work for all pages. After submitting the page the drop down will have no items bound to it.
To enable view state for a particular page , we have to enable it on both Master page, and the Aspx Page. If the drop down list is placed in a user control, then we have to enable the view state in the user control also, other wise the view state of the drop down will get disabled.
Even if it is set to true in all, but disabled in the control, then it get disabled but the converse is false.
Conclusion:If you want to enable view state on dropdown list when the view state get disabled in web.config file; We should explicitly enable it on the Master page, Aspx Page where it’s being declared and the user control(if it is declared in User control). Then only the viewstate of the control get enabled.
If Viewstate enabled in web.config and disabled in either of master page or Aspx Page, then viewstate of dropdown list get disabled. If the view state enabled on both Master page and Aspx Page, then no need to explicitly enable it in user control and the control itself.

1 comment:

sasikumarreddy valipireddy said...

Good explanation. Thanks