Friday, March 25, 2011

NamingContainer

This article here http://aspadvice.com/blogs/joteke/archive/2007/02/25/Understanding-the-naming-container-hierarchy-of-ASP.NET-databound-controls.aspx decribes the NamingContainer property.

When using a TemplateField in a GridView i.e. when you use... a checkbox to update the database OnCheckChanged of the checkbox


<asp:TemplateField HeaderText="active">
            <ItemTemplate>
            <asp:CheckBox ID=chkEmpActive AutoPostBack=true Checked='<%#Eval("active")%>' runat=server OnCheckedChanged=chkEmpActive_CheckedChanged />
            <asp:HiddenField ID=hidActive runat=server Value='<%#Eval("emp_id")%>' />
            </ItemTemplate>
 </asp:TemplateField>

its easier to have a hidden field with it;helps with the update and to find it,  the NamingContainer of the CheckBox can be used to get to the hiddenfield

when you do


protected void checkbox1_oncheckchange(object sender, EventArgs e)
{
    CheckBoc chk =(CheckBox)sender;
    GridViewRow row = (GridViewRow)chk.NaminConatiner;
    HiddenField hidEmp = (HiddenField)row.FindControl("hidActive");
    empNo = HiddenField.Value; // where empNo is a global var
}

you can use the empNo variable in JavaScript

<script>
var empno = <%=empNo%>

//and use it some calculations

</script>

I used the empNo to call an ASP page using the httpXml object and do some database operations





No comments:

Post a Comment