You must Sign In to post a response.
  • Category: ASP.NET

    How find DataGrid HeaderTemplate control of checkbox?

    Hi,

    I am trying import datagrid to excel within selected
    coloms. I am trying to find header checkbox. but it not getting.
    I am using datagrid not gridview.

    html code
    ==========
    <asp:DataGrid id="dgDetails" AutoGenerateColumns="false" Font-Size="13px" ShowFooter="true" AllowPaging="true"
    CellPadding="1" CellSpacing="1" PagerStyle-Mode="NumericPages" PageSize="20"
    OnPageIndexChanged="dgFarmerDetail_PageIndexChanged">
    <FooterStyle BackColor="#293339" ForeColor="White" />
    <HeaderStyle CssClass="pageheading" HorizontalAlign="Center" BackColor="#293339"
    Font-Bold="True" ForeColor="White" Width="10%" Font-Size="Medium" />
    <AlternatingItemStyle CssClass="pageheading" BackColor="White" ForeColor="#284775" />
    <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <PagerStyle Mode="NumericPages" BackColor="#999999" Font-Size="Medium" ForeColor="Black"
    CssClass="pager" />
    <SelectedItemStyle BackColor="#F7F6F3" Font-Bold="True" ForeColor="#333333" />
    <Columns>
    <asp:TemplateColumn>
    <HeaderTemplate>
    <asp:CheckBox ID="chkCol0" runat="server" Checked="true" />
    <asp:Label ID="lblCol0" runat="server" Text="????????? ???" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label ID="lblFARMERNAME" runat="server" Text='<%# Eval("FARMER_NAME")%>' />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>
    <asp:CheckBox ID="chkCol1" runat="server" Checked="true" />
    <asp:Label ID="lblCol1" runat="server" Text="?????? ????." />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label ID="lblCONTACTNO" runat="server" Text='<%# Eval("CONTACT_NO")%>' />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>
    <asp:CheckBox ID="chkCol2" runat="server" Checked="true" />
    <asp:Label ID="lblCol2" runat="server" Text="?-???" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label ID="lblEMAILID" runat="server" Text='<%# Eval("EMAIL_ID")%>' />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>
    <asp:CheckBox ID="chkCol3" runat="server" Checked="true" />
    <asp:Label ID="lblCol3" runat="server" Text="????? ????." />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label ID="lblSURVEYNO" runat="server" Text='<%# Eval("SURVEY_NO")%>' />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>
    <asp:CheckBox ID="chkCol4" runat="server" Checked="true" />
    <asp:Label ID="lblCol4" runat="server" Text="??? ?????? ????." />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label ID="lblHISSA_NO" runat="server" Text='<%# Eval("HISSA_NO")%>' />
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>

    code :
    =======
    private void GetCheckBoxStatus()
    {
    CheckBox chkCol0, chkCol1, chkCol2, chkCol3, chkCol4;

    try
    {

    Control HeaderTemplate = dgFarmerDetail.Controls[0].Controls[0];
    chkCol0 = (CheckBox)HeaderTemplate.FindControl("chkCol0");

    //chkCol0 = (CheckBox)dgFarmerDetail.Controls[0].Controls[0].FindControl("chkCol0");


    if (chkCol0.Checked == true)
    {
    //string mes = "hi";
    }

    }
    catch (Exception ex)
    {
    throw ex;
    }
    }
  • #768863
    Use can try this code snippet to find DataGrid HeaderTemplate control of checkbox
     
     <asp:DataGrid ID="DataGrid1" runat="server"
    DataSourceID="SqlDataSource1" onitemcreated="DataGrid1_ItemCreated"
    onitemdatabound="DataGrid1_ItemDataBound" ClientIDMode="Static" >
    <Columns>
    <asp:TemplateColumn HeaderText="test">
    <HeaderTemplate >
    <asp:CheckBox ID="CheckBox2" runat="server" ClientIDMode="Static"
    oncheckedchanged="CheckBox2_CheckedChanged" />

    </HeaderTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>

  • #768869
    Hai Chandrashekhar,
    The code line below:

    Control HeaderTemplate = dgFarmerDetail.Controls[0].Controls[0];

    Will not be able to find the control as the Control[0] will be the wrapper/outer control which is the column.
    So you can try using :

    Control HeaderTemplate = dgFarmerDetail.Controls[0].Controls[1];

    This will give you the checkbox control.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments