A number of queries, articles from developers rise on this issue. 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 followign 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(); } We select an item in the dropdownlist after the page has loaded and the list has been populated. Then, we have button
and on its click event, we want the selected item in the dropdownlist to be displayed. Guess what you will get as selected item no matter what you select from the dropdownlist? The first item or the default item such as "Select an option". The issue is due
to the postback that occurs when the button is clicked. ASP.NET server controls are posted back to the server for all events and hence, the page will be reloaded - thereby the dropdownlist again populated and the selected item would be the first default item.
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. This is applicable to .NET version 1.0, 1.1 since in .NET 2.0 (CodeName: Whidbey), this has been handled automatically
to check postbacks and normal page_loads.
Edited by SomeNewKid. Please post code between <code> and </code> tags.
I have a very big problem with "__VIEWSTATE" hidden field. Before digging deep into the problem, let me brief you something. In ASP.NET the "Page.IsPostback" property relies on "__VIEWSTATE" hidden field i.e. if the posted forms contains "__VIEWSTATE" field
then "Page.IsPostback" is set to true. the real problem lies here, I need some code to be executed before page is posted back.
in page_load
{
if(!IsPostback)
{
///My code goes here
}
}
Is there a alternate way of finding whether the page is posted back.
Exatly i ned to know form which page this page got loaded.
-----------------------------------------------
If you can't be able to get what my real problem is try this,
and explain me what really is happening?.
create three files like this:
------------------
Form1: Name: [PATH]/posting_form.htm
public class catched_form : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
private void Page_Load(object sender, System.EventArgs e)
{
if(Page.IsPostBack)
{
///The content here get's skipped from executing
}
else
{
///This Part is getting Executed.
}
}
#region Web Form Designer generated code
private void Button1_Click(object sender, System.EventArgs e)
{
///This Part is getting Executed.
Response.Write("Button1.Clicked");
}
private void Button2_Click(object sender, System.EventArgs e)
{
///This Part is getting Executed.
Response.Write("Button2.Clicked");
}
}
-------------------------------------------- Can any one explain me how the controls's were recognaized by asp.net. why the
Button2_Click event is getting fired. if ASP.NET has such a vulnerability to hacker's what will be the extent of damage? ----------------------------------
ranganh
Star
12085 Points
2435 Posts
Microsoft
Handling Page Load Events and PostBack Issues
Dec 18, 2004 12:27 PM|LINK
Harish
http://geekswithblogs.net/ranganh
dheeraj.m
Member
20 Points
4 Posts
Re: Handling Page Load Events and PostBack Issues
Jan 13, 2005 04:40 AM|LINK
I have a very big problem with "__VIEWSTATE" hidden field. Before digging deep into the problem, let me brief you something. In ASP.NET the "Page.IsPostback" property relies on "__VIEWSTATE" hidden field i.e. if the posted forms contains "__VIEWSTATE" field then "Page.IsPostback" is set to true. the real problem lies here, I need some code to be executed before page is posted back.
in page_load { if(!IsPostback) { ///My code goes here } }Is there a alternate way of finding whether the page is posted back. Exatly i ned to know form which page this page got loaded. ----------------------------------------------- If you can't be able to get what my real problem is try this, and explain me what really is happening?. create three files like this: ------------------ Form1: Name: [PATH]/posting_form.htm ------------------- FORM2: Name: [PATH]/catched_form.aspx -------------------- CODE_BEHIND: [PATH]/catched_form.aspx.cspublic class catched_form : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Button Button2; private void Page_Load(object sender, System.EventArgs e) { if(Page.IsPostBack) { ///The content here get's skipped from executing } else { ///This Part is getting Executed. } } #region Web Form Designer generated code private void Button1_Click(object sender, System.EventArgs e) { ///This Part is getting Executed. Response.Write("Button1.Clicked"); } private void Button2_Click(object sender, System.EventArgs e) { ///This Part is getting Executed. Response.Write("Button2.Clicked"); } }-------------------------------------------- Can any one explain me how the controls's were recognaized by asp.net. why the Button2_Click event is getting fired. if ASP.NET has such a vulnerability to hacker's what will be the extent of damage? ----------------------------------