Tuesday, July 21, 2009

IsPostBack property

IsPostBack-Gets a value indicating whether the page is being loaded in response to a client postback, or if it is being loaded and accessed for the first time.

when the page is loaded in order to determine whether the page is being rendered for the first time or is responding to a postback. If the page is being rendered for the first time, the code calls the Page..::.Validate method.

Syntax:


private void Page_Load()
{
if (!IsPostBack)
{
// Validate initially to force asterisks
// to appear before the first roundtrip.
Validate();
}
}


Example


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PageDataBind();
}
else
{
Response.write("2nd Time Visisted..");
}
}

protected void PageDataBind(string SearchProduct)
{
SqlDataAdapter SDA ;
DataSet ds = new DataSet();
String sql = "select * from Emp where ID ='"+ DropDownList.SelectedItem.Text +"'";
SDA.Fill(ds, "productDetails");
ShowId.DataSource = ds;
ShowId.DataBind();
}


when loading of the page, the control doesn't go through the redundant action of re-loading the control. It loads it only once, at the initial page load. Once the page is loaded, the click event from the button (or however you post back) is considered to be a postback. Yes, the page gets reloaded, but, by surrounding your data population with this statement, the page knows that the initial data loading was already done, so it doesn't happen any more, and it gets maintained throughout any subsequent post back. The second, and most rewarding thing that happens is that the selection which was made by the end user is maintained throughout the post back. This is the pay-off - by doing it with the If/Then Page.IsPostBack statement, your selection carries through to your query and the query returns the results you wanted in the first place.

No comments:

Post a Comment