Effects of keeping AutoEventWireup "true" or "false"


When AutoEventWireup is true,  .Net automatically associates page events and methods to event handlers as per event name. i.e. For Load and Init event, Page_Load and Page_Init handler is considered, respectively. No explicit Handles clause or delegare is needed.

When you set AutoEventWireup="false",  you have to add an event handler for your page event.

Below code helps you understand everything about it.

namespace AutoEventWireUpCheck
{
    public partial class _Default : System.Web.UI.Page
    {
        public _Default()
        {
            this.Load += new EventHandler(_Default_MyPageLoad);
        }

        protected void _Default_MyPageLoad(object sender, EventArgs e)
        {
            //Do anything
            int i = 0;
            System.Console.WriteLine("From _Default_Load event");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Do anything
            int i = 0;
            System.Console.WriteLine("From page load event");
        }
    }
}

This will call _Default_MyPageLoad event on page load. (Page_Load will not be called)
Here, keeping AutoEventWireup="false" and not defining event handler for Load event will not call any event handler though you’ve written Page_Load event.

keeping AutoEventWireup="true" will call Page_Load event.
If you keep
(1) AutoEventWireup="true",
(2)
public _Default()
{
this.Load += new EventHandler(_Default_MyPageLoad);
}
 Code and
(3)
define both event handler, both event handler will be called.

Posted in |

0 comments:

Post a Comment