无法在datagrid ASP.NET中获取复选框值

时间:2014-10-06 13:07:27

标签: c# asp.net webforms

我有一个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
            }

        }

感谢。

2 个答案:

答案 0 :(得分:1)

试试这个

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

                if (chkVjezba.Checked)
                {
                    //something
                }
            }

        }

答案 1 :(得分:1)

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

时才进行数据绑定