c# - 在代码隐藏中为网格中的复选框分配一个值

标签 c# asp.net

我在 asp.net 中有以下网格

<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>

我想在后面的代码中为“chk_DWlocReq”赋值

像这样

foreach (GridViewRow dgvr in grdDWlocations.Rows)
{
    (CheckBox)dgvr.FindControl("chk_DWlocReq")=true;                                                 
}

但是上面一个无效,这怎么能正常呢?

最佳答案

我认为您的代码必须更改为:

foreach (GridViewRow dgvr in grdDWlocations.Rows)
{
    ((CheckBox)dgvr.FindControl("chk_DWlocReq")).Checked=true;
}

关于c# - 在代码隐藏中为网格中的复选框分配一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37177399/

相关文章:

c# - Object-C 和 C# 中的 MD5

c# - Count是否实现了一个IEnumerable

c# - 如何判断文件是否已下载?

asp.net - Visual Studio 2008 sql 查询语法;去掉括号

c# - 获取存储过程程序的表名

c# - 将自定义类的集合添加到 Settings.Settings

c# - 为本地报告设置数据源 - .NET 和报告查看器

asp.net - 每次加载 DIV 时执行代码隐藏方法(ASP、ascx、ascx.cs)

c# - 解析 XML 文件时出现问题

c# - 无法从 NULL 读取属性 "Patient"