Here this article on dynamic event assignment will explain you how the ASP.Net event system works and fires an event.
We will take following things to be covered
- Taking System.Web.UI.WebControls.Button as a testing Control
- Flow of HTTP protocol and execution/handling of event
- Failure of ASP.Net Event handling
General
When there .Net Framework was not introduced we had to check for the value of the HTML controls to process further and had to maintain the state of the INPUT control of HTML in multiple POSTs
The old way in Classic ASP to get the value of input element and to work on that Request.Form("InputElementName")
Where ASP.Net takes care of the stuff which we had to do in Classic ASP like maintaining state of the HTML elements between multiple POSTs.
Taking System.Web.UI.WebControls.Button as a testing Control
Creating Button control dynamically and adding on the Form following way and assign Click event to it,
Code in the Page_Load method will be called every time the Form Post
btn_Click method will be executed whenever the Dynamically created Button clicked.
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.ID = "btn" + i.ToString();
btn.Text = "Button " + i.ToString();
btn.Click += new EventHandler(btn_Click);
this.form1.Controls.Add(btn);
}
}
void btn_Click(object sender, EventArgs e)
{
//Get the button index so we can identify that which button has been clicked..
//because click event is same for every button
int btnIndex = Convert.ToInt32(((Button)sender).ID.Substring(3, 1));
Response.Write(btnIndex.ToString());
}
Flow of HTTP protocol and execution/handling of event
Basic flow is as follows
- Browser sends HTTP request to IIS
- IIS looks for the path on the server, and forwards the request to .Net CLR
- Where CLR looks for the application domain and forwards the request to respective domain
- .Net CLR process the request and revert back with the response and give it back to IIS and IIS to client.
Now see how events get handled in above case
If Page directives "EnableViewState" and "EnableEventValidation" is set to true( by default these are set to true )
- Each control has ClientID property, which used to keep the track while execution of the event
- When you assign event handler to any control(e.g. Button) it adds an entry to the Page's hidden input element "__EVENTVALIDATION" stating that which event is bound to which control,
This will be in only case when "EnableEventValidation" is set to "true"
- When fresh request has made the response will have hidden input element named "__EVENTVALIDATION" with the event mapping information, this mechanism reduces the risk of unauthorized postback requests and callbacks.
Actually this information is used to validate the request on the server for the following security reason.
- No one can make false request and invoke the server side method.
- Using "EnableEventValidation" is good idea to increase security but this puts a load on bandwidth, because your page size increases.
- When Button is clicked the HTML Page (Actually it is HTML but for server it is ASP.Net) posts the information to the server,
where on server Page_Load method is going to call again and event will be assigned as per the code.
but not exactly it will reassign the event... but it will reevaluate the HTTP Request and will map the fired event with the existing one and executes the assigned method.
Note: "EnableEventValidation" actually not used to execute/mapping the event on the server, but it is allows to match the what event(s) was assigned before the PostBack and what event has been assigned on the server, and reduces the risk of unauthorized postback requests and callbacks.
Failure of ASP.Net Event handling
In some cases developers faces some issues regarding firing of event has been not working....
The root cause behind this is, while PostBack the control doesn't have event assignment will not able to fire up the event.
See the following code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.ID = "btn" + i.ToString();
btn.Text = "Button " + i.ToString();
btn.Click += new EventHandler(btn_Click);
this.form1.Controls.Add(btn);
}
}
}
void btn_Click(object sender, EventArgs e)
{
int btnIndex = Convert.ToInt32(((Button)sender).ID.Substring(3, 1));
Response.Write(btnIndex.ToString());
}
In above scenario Page_Load method doesn't creating mapping for the dynamically created buttons and not able to fire event because it will only be executed when there is no PostBack.
Disclaimer:Author is not responsible if any kind of provided information may false