Cheap Hosting Domain Names

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 20 April 2012

AutoEventWireup in ASP.NET: Why my ASP.NET events fire twice?

Posted on 08:50 by Unknown
Here is a brief description of AutoEventWireup attribute in ASP.NET.

1. AutoEventWireup is an attribute in Page directive. 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

We can specify the default value of the AutoEventWireup attribute in the following locations:

1. The Machine.config file.
2. The Web.config file.
3. Individual Web Forms (.aspx files).
4. Web User Controls (.ascx files)

If you make these changes in the Machine.config file, the changes affect all ASP.NET Web Forms on the computer. If you make these changes in the Web.config file, the changes affect only the application that the file belongs to. However, to make changes in the individual Web Form Only, we have to add the AutoEventWireup attribute to the @ Page directive, as shown above.

2. AutoEventWireup is a Boolean attribute that indicates whether the ASP.NET pages events are auto-wired.

3. AutoEventWireup will have a value true or false. By default it is true in C# and false in VB.NET.
VB.NET has a mechanism for defining an event handler and subscribing to an event with the 'Handles' keyword.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub

There is no equivalent to the Handles keyword in C#. Thats why AutoEventWireup is by default true in C#.

protected void Page_Load(object sender, EventArgs e)
{
}

AutoEvenWireup = "TRUE"

The ASP.NET page framework supports an automatic way to associate page events and methods. If you do set AutoEventWireup to true, Visual Studio will generate code to bind the events and the page framework will automatically call events based on their names. In that case, no explicit Handles clause or delegate is needed.

AutoEventWireup = "FALSE"

If you do set AutoEventWireup to false, Visual Studio will not be able to generate code to bind the events. In this case, you must define explicit Handles clause or delegate.

public partial class _Default : Page
{
   public _Default()
   {
      Load += new EventHandler(Page_Load);
      PreInit += new EventHandler(Page_PreInit);        
   }
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
   protected void Page_PreInit(object sender, EventArgs e)
   {
     
   }
}

Why my ASP.NET events fire twice?

A very common question asked. The simple answer to this question is:

If you have AutoEventWiureUp="TRUE" as well as you also have defined delegates for the event handling in your code, .NET Framework will automatically call methods based on their names and also the methods you defined explicitly.

Disadvantage of AutoEventWireup = "TRUE"

1. The disadvantage of the AutoEventWireup attribute is that it requires that the page event handlers have specific, predictable names. This limits your flexibility in how you name your event handlers.

2. If you do set AutoEventWireup to true and also defined event handlers, Visual Studio will generate code to bind the events and the page framework will automatically call events based on their names. This can result in the same event code being called twice when the page runs. As a consequence, you should always leave AutoEventWireup set to false when working in Visual Studio.

3. Performance Issue: Another disadvantage is that performance is adversely affected, because ASP.NET searches for methods at run-time. For a Web site with high traffic volumes, the impact on performance could be significant.
Email ThisBlogThis!Share to XShare to Facebook
Posted in DOTNET | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Online Religion Degrees
    Religion plays a major role in the historical, political, and cultural life of our societies. If you are fascinated by belief systems, consi...
  • How to create dynamic tables in HTML using javascript at runtime?
    In this tutorial, you will come to know how to create dynamic tables in HTML. There are some situations when you don't know the contents...
  • Online Economics Degrees
    Economists research and analyze economics, or the way people choose to use their resources to produce goods and services. Economists typical...
  • Online Advertising Degrees
    The objective of any business firm is to market and sell its products or services profitably. In small firms, the owner or chief executive o...
  • Online Geography Degrees
    With the growth of online colleges and universities, geography degrees online have gained immense importance. Geography degrees online can b...
  • Online DBA (Database Administrator) Degrees
    Database managers keep vital information organized.  With a database technology degree, you may find yourself organizing customer informatio...
  • Online Social Science Degrees
    Social science covers a broad range of fields: behavioral science, history, economics, geography, political science, women''s studie...
  • Difference between page_init, page_load and page_prerender events
    page_init This event is the first event to occur when an ASP.NET page is executed. This is the event where you should be performing any init...
  • Online BCA Degrees
    The boom in the IT industry has opened up plenty of job opportunities for computer professionals. As a result, computer application courses ...
  • Online Professional Degrees
    Going back to get more education can be a difficult, time consuming and sometimes impossible proposition. But with new advances in virtual t...

Categories

  • AJAX
  • C++
  • CSS
  • Delphi
  • DOTNET
  • HTML
  • Javascript
  • jQuery
  • Management
  • Online Degrees
  • Oracle
  • Others
  • Phonegap
  • PHP
  • Unix
  • XML

Blog Archive

  • ▼  2012 (180)
    • ►  September (89)
    • ►  August (11)
    • ►  July (4)
    • ►  June (3)
    • ►  May (25)
    • ▼  April (48)
      • DOM Events: Mouse Events, Keyboard Events, Form Ev...
      • What is DOM (Document Object Model): Tree and Node...
      • DocType: Strict, Transitional and Frameset
      • How the ASP.NET authentication process works?
      • Response.Redirect vs Server.Transfer: What to use ...
      • GET vs POST: Which one is better? A 10 point compa...
      • HTTP vs HTTPS: Similarities and Differences
      • How IIS processes ASP.NET request?
      • AutoEventWireup in ASP.NET: Why my ASP.NET events ...
      • Web Farms in ASP.NET: Advantages and Issues
      • Cautions while dropping a tablespace
      • 12 Point Comparision between FTP and HTTP Protocol...
      • Dataset, Dataview, Datatable and common operations...
      • Client Side State Management in ASP.NET
      • Difference between page_init, page_load and page_p...
      • Database FLASHBACK mode: Overview
      • Database ARCHIVELOG mode: Overview
      • Hash Collision Attacks in .NET
      • ADO.NET: A quick revision
      • What is SQL Injection?
      • What is ASP.NET AJAX?
      • What is DLL HELL?
      • DIV vs TABLE tag: Which one to use?
      • Exception Handling in ASP.NET
      • Business Intelligence (BI): Data Warehouse, Data M...
      • UDDI: Universal Description, Discovery and Integra...
      • Web Services: Exposing and Consuming
      • Web Service Description Language
      • Simple Object Access Protocol
      • Caching in ASP.NET
      • Partial Classes in ASP.NET
      • Difference between DLL and EXE Files
      • What is an ASP.NET User Control?
      • Page Directive in ASP.NET
      • DOTNET Framework: CLR, CTS and CLS
      • What is Tethering and Hotspot?
      • Preventing Caching in AJAX URLs
      • What is web.config file? What is the significance ...
      • HTML vs XHTML vs DHTML
      • AJAX - A quick revision
      • Web Server vs Application Server vs Database Server
      • Difference Between ASP.NET Server Controls and HTM...
      • web.config vs app.config vs machine.config
      • Assemblies in .NET Framework
      • Silver Bullets for Testing
      • Unix Commands which should be on tips of each deve...
      • Basics of IBM Websphere MQ (Part 1)
      • What is Garbage Collector? How and when does it run?
Powered by Blogger.

About Me

Unknown
View my complete profile