Close AD

Chintan Patel's Blog
Home | Archive | Photo Albums | Links | Contact Sign In | Sign Up

About Author

Chintankumar Patel
09 Jun, 2008

Contact Me  

Working as a Technical Consultant for Conchango.

Having experience in to IT from 7+ years and working on Microsoft Technologies

Archive

2009 Dec   (1)
2008 Dec   (1)
2008 Oct   (3)
2008 Sep   (6)
2008 Aug   (1)
2008 Jul   (2)
2008 Jun   (7)

Recent Posts

Windows 7 review
Comments : 0
Not Rated  
Disable System Restore in Windows Vista
Comments : 3
Not Rated  
What's New in the .NET Framework 2.0 ?
Comments : 2
Not Rated  
How to work with partitions in Windows Vista / XP when Disk Management doesn’t work
Comments : 3
Not Rated  
How to resize a partition in Windows Vista?
Comments : 3
Not Rated  
Top 10 tricks for handling null values in Microsoft Office Access
Comments : 17
Not Rated  
What is Vista's ReadyBoost and SuperFetch Technology
Comments : 5
Not Rated  
What is YouTube? - An introduction to the YouTube.com
Comments : 5
Not Rated  
SQL DATEDIFF Function - Applies to MS SQL Server and MS Office Access
Comments : 4
Not Rated  
Booting from USB Pen/Key/Flash Drive (Windows/Linux)
Comments : 5
Not Rated  

Categories

.Net Graphics   (2)
.Net Technology   (6)
ASP.Net   (1)
General   (2)
Microsoft Access   (1)
Microsoft Visual Studio   (1)
Microsoft Windows   (4)
SQL   (1)
USB   (1)
Windows 7   (1)
Windows Vista   (2)

Tags

.Net , .Net Framework 2.0 , .Net Graphics , Asp.Net , Boot , C# , Class Library , Coding Standards , Convert File System , Database , DATEDIFF , DATEDIFF Function , Disk Management , Embedded Resources , EnableEventValidation , EnableViewState , EncoderParameter , FAT32 to NTFS , Form Authentication , HDD , High Quality Thumbnail , Intellisense , Linux , Microsoft Access , Microsoft's SQL , Partition , ReadyBoost , ReSharper , Resize Image , Resize Partition , Security , SQLDataReader , SuperFetch , System Restore , USB , Usb device not recognized , Vista , Visual Studio 2005 , What's New , Windows , Windows 7 , Windows Application , Windows Errors , Windows Vista , Windows XP , YouTube

Tag : enableviewstate (1 posts)

Dynamic Event(e.g. Button Click) creating and handling in C# ASP.Net

Fri, 27 Jun, 2008

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


  

Powered by NineOn Inc.