Thursday, December 10, 2009

AutoEventWireup attribute in ASP.NET

. The ASP.NET page framework supports an automatic way to associate page events and methods. If the AutoEventWireup attribute of the Page directive is set to true, the page framework calls page events automatically, specifically the Page_Init and Page_Load methods. In that case, no explicit Handles clause or delegate is needed.

. ASP.NET pages automatically bind page events to methods that have the name Page_event. This automatic binding is configured by the AutoEventWireup attribute in the @ Page directive, which is set to true by default. If you set AutoEventWireup to false, the page does not automatically search for methods that use the Page_event naming convention.
So, if I wanted to handle the Page Init event, I just need to add this and the event gets bound automatically.

protected void Page_Init() {
Trace.Write("I am in init!");
}

void Page_Init() {
Trace.Write("I am in init!");
}

.
AutoEventWireup="true" target is for page events only. In case of AutoEventWireup method are not case sensitive. (Page_Load or page_load both will work).

If AutoEventWireup="false" but still you want to executed Page event (Page_Load). In this you have to explicitly code for it.

<form id="form1" runat="server" onload="Page_Load">





Wednesday, November 11, 2009

Gridview Header CheckBox click selects all the item CheckBoxes

Below is the JavaScript function which is responsible to Select or Deselect all the CheckBoxes when Header CheckBox is clicked....
HeaderCheck(CheckBox)
{
cnt=0;
var TargetBaseControl = document.getElementById('ctl00_ContentPlaceHolder1_GridView1');
var TargetChildControl = 'ChkSel';
var Inputs = TargetBaseControl.getElementsByTagName('input');
for(var n = 0; n < type ="=">= 0)
{
Inputs[n].checked = CheckBox.checked;
}
}

Call this function in GridView's Header CheckBox onclick............Below is the code snippet
asp:templatefield
headertemplate
asp:templatefield
headertemplate
asp:checkbox id="ChkHeader" runat="server" onclick="return HeaderCheck(this);"
asp:checkbox
itemtemplate
asp:checkbox id="ChkSel" runat="server"
asp:checkbox
itemtemplate
headertemplate asp:templatefield
asp:checkbox id="ChkHeader" runat="server" onclick="return HeaderCheck(this);"
asp:checkbox
itemtemplate
asp:checkbox id="ChkSel" runat="server"
asp:checkbox
itemtemplate
headertemplate asp:templatefield