Wednesday, September 23, 2015

What is AutoEventWireup in asp.net

Suggested Videos
Part 130 - Refreshing cache automatically, when cached data is removed
Part 131 - Cache dependency on sql server database table 
Part 132 - Reload data into cache automatically when data in the table changes



Let us understand the use of AutoEventWireup property with an example. Create an asp.net web application project.

A webform in asp.net raises several events in it's life cycle. The following are few of those events.
1. Page Load
2. Page Load Complete
3. Page PreRender
4. Page PreRenderComplete



Copy and paste the following code in WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Page Load <br/>");
}

protected void Page_LoadComplete(object sender, EventArgs e)
{
    Response.Write("Page LoadComplete <br/>");
}

protected void Page_PreRender(object sender, EventArgs e)
{
    Response.Write("Page PreRender <br/>");
}

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
    Response.Write("Page PreRenderComplete <br/>");
}

Now in WebForm1.aspx, set AutoEventWireup=true
Run the application, and notice that the above event handler methods are executed as expected. We did not explicitly associate event handler methods to the events, but still the event handler methods are hooked up to their respective events. This is because we have AutoEventWireup attribute set to true.

Now, set AutoEventWireup=false
Run the application, and notice that none of the above event handler methods are executed. This is because, when "AutoEventWireup" property is set to false, the event handler methods does not get automatically associated with their respective events. We have to explicitly associate them by overriding OnInit() method as shown below. Now, copy and paste the following code in webform1.aspx.cs
protected override void OnInit(EventArgs e)
{
    this.Load += new EventHandler(Page_Load);
    this.LoadComplete += new EventHandler(Page_LoadComplete);
    this.PreRender += new EventHandler(Page_PreRender);
    this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
}

Run the application, and notice that the above event handler methods are executed as expected. 

Now, set AutoEventWireup=true

Run the application, and notice that every event handler method is executed twice. This is because, 
1. Setting AutoEventWireup=true, registered the event handler method's once, and
2. Overriding OnInit() method, has registered the same event handler method again

Important points to remember:
1. When AutoEventWireup is set to true and if you want the event handlers to be wired up with their events automatically, the event handler names should follow the standarad naming convention - Page_EventName.
2. AutoEventWireup can be set in the page directive or in web.config file.
3. To set autoEventWireup in web.config, use pages element as shown below.
<configuration>
   <system.web>
      <pages autoEventWireup="true" />
   </system.web>
</configuration>
4. If autoEventWireup, is set at both webform and web.config level, webform setting will take precedence over web.config setting.

So, AutoEventWireup is a boolean property which, when set to true, the page event handler methods are automatically wired with their respective events. If this property is set to false, then the event handler methods need to be explicitly associated with their respective events.


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

Related Posts:

  • SQL WHERE ClauseThe WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill a specified criterion. SQL WHERE Syn… Read More
  • SQL AND & OR OperatorsThe AND & OR operators are used to filter records based on more than one condition. The AND & OR OperatorsThe AND operator displays a record… Read More
  • SQL JOINThe JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. … Read More
  • SQL UNION OperatorThe SQL UNION Operator The UNION operator is used to combine the result-set of two or more SELECT statements. Notice that each SELECT statement with… Read More
  • SQL SELECT INTO StatementThe SQL SELECT INTO statement can be used to create backup copies of tables. The SQL SELECT INTO StatementThe SELECT INTO statement selects data from … Read More

0 comments:

Post a Comment