Use of AutoEventWireup property


When creating asp.net web page, in the html view there is AutoEventWireup=true what does it means.

It indicates that page event automatically binds with event handlers.

<%@ Page Language=”C#” AutoEventWireup=”true”  CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
    <title></title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:TextBox ID=”txtPageLoadEventHandler” runat=”server”></asp:TextBox>
        <asp:TextBox ID=”txtButtonClickEventHandler” runat=”server”></asp:TextBox>
        <asp:Button ID=”btnTest” runat=”server” Text=”Button” onclick=”btnTest_Click” />
        </div>
    </form>
</body>
</html>

If the btnTest is clicked then the event handlers automatically called. as AutoEventWireup=”true”.


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtPageLoadEventHandler.Text = "Inside Page_Load event handler";
}
protected void btnTest_Click(object sender, EventArgs e)
{
txtButtonClickEventHandler.Text = "Inside btnTest_Click event handler";
}
}

Now if use the same code above with AutoEventWireup=”false”. It shows that button click handler is called but page load is not called.

Now to call page load event handler event when AutoEventWireup=”false”. Have to explicitly bind the page load event with page load event handler like below :


public partial class _Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.Load+=new EventHandler(Page_Load);
}

protected void Page_Load(object sender, EventArgs e)
{
txtPageLoadEventHandler.Text = “Inside Page_Load event handler”;
}
protected void btnTest_Click(object sender, EventArgs e)
{
txtButtonClickEventHandler.Text = “Inside btnTest_Click event handler”;

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: