代码之家  ›  专栏  ›  技术社区  ›  kez Sirwan Afifi

为网格标题中的复选框指定值

  •  0
  • kez Sirwan Afifi  · 技术社区  · 9 年前

    我有以下网格

    <asp:GridView ID="grdDWlocations" CssClass="table table-hover table-striped" runat="server" GridLines="None" ShowHeaderWhenEmpty="True"
        EmptyDataText="No data found..." AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField HeaderText="" Visible="true">
                <HeaderTemplate>
                    <asp:CheckBox ID="allDWlocchk" runat="server" Checked="true" Width="10px" onclick="CheckAllgrdReqDW(this)"></asp:CheckBox>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chk_DWlocReq" runat="server" Checked="true" Width="5px" OnCheckedChanged="chk_Req_CheckedChangedDW_Click" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Code">
                <ItemTemplate>
                    <asp:Label ID="lbl_DWCode" runat="server" Text='<%# Bind("Ml_loc_cd") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Description">
                <ItemTemplate>
                    <asp:Label ID="lbl_DWDescription" runat="server" Text='<%# Bind("Ml_loc_desc") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    我想为“allDWlocchk”赋值,它是头检查,

    我怎么能在代码背后做到这一点

    我试过了,但没用

    尝试1:

    ((CheckBox)(grdDWlocations).FindControl("allDWlocchk")).Checked = false;  
    

    尝试2:

    ((CheckBox).FindControl("allDWlocchk")).Checked = false; 
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   Andrei    9 年前

    你在正确的轨道上, FindControl 就是到这里的路。但问题是,它只适用于直系子女。因此,您需要在标题行中搜索,而不是在Page或GridView上搜索。它可用作GridView属性。因此:

    GridViewRow headerRow = grdDWlocations.HeaderRow;
    ((CheckBox)headerRow.FindControl("allDWlocchk")).Checked = false;