可以';t在数据网格ASP.NET中获取复选框值

本文关键字:NET ASP 获取 复选框 网格 数据网 数据 可以 | 更新日期: 2023-09-27 18:25:50

我有一个DataGrid,它有用于选择特定用户的复选框。当单击"确定"按钮时,我应该得到复选框的状态,如果选中,则得到行的ID。我到达了复选框,但值总是Checked=false,当然,即使我选中了复选框。代码如下:

.aspx

        <Columns>
            <asp:TemplateColumn HeaderText="Odabir">
                <ItemTemplate>
                    <asp:CheckBox ID="chkVjezba" runat="server" Enabled="true"/>
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn HeaderText="Vježba" DataField="Naziv"></asp:BoundColumn>
            <asp:TemplateColumn HeaderText="Broj serija">
                <ItemTemplate>
                    <asp:TextBox ID="brojSerija" runat="server" Width="20px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Ocjena 6">
                <ItemTemplate>
                    <asp:TextBox ID="ocjena6" runat="server" Width="20px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Ocjena 7">
                <ItemTemplate>
                    <asp:TextBox ID="ocjena7" runat="server" Width="20px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Ocjena 8">
                <ItemTemplate>
                    <asp:TextBox ID="ocjena8" runat="server" Width="20px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Ocjena 9">
                <ItemTemplate>
                    <asp:TextBox ID="ocjena9" runat="server" Width="20px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Ocjena 10">
                <ItemTemplate>
                    <asp:TextBox ID="ocjena10" runat="server" Width="20px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>

我的.cs代码文件:

foreach (DataGridItem row in vjezbeGrid.Items)
            {
            if (((CheckBox)row.Cells[0].FindControl("chkVjezba")).Checked)
            {
               //something
            }
        }

谢谢。

可以';t在数据网格ASP.NET中获取复选框值

您可以在每次回发时进行数据绑定。请确保仅在以下情况下进行数据绑定!Page.IsPostBack.

像这样尝试

foreach (DataGridItem row in vjezbeGrid.Items)
        {
            if (row.ItemType == ListItemType.Item)
            {
                CheckBox chkVjezba = (CheckBox)row.FindControl("chkVjezba");
                if (chkVjezba.Checked)
                {
                    //something
                }
            }
        }