【问题标题】:Having trouble getting Checkbox value from GridView in Edit mode在编辑模式下从 GridView 获取复选框值时遇到问题
【发布时间】:2014-09-21 02:41:25
【问题描述】:

我正在尝试在网格视图中使用复选框来更新我的数据库。一切似乎都运行良好,除了在编辑模式下从复选框中获取正确的值。

这里是相关的aspx代码:

<asp:TemplateField HeaderText="Unlock" SortExpression="Unlock">
    <EditItemTemplate>
        <asp:CheckBox ID="chkEditUnlock" runat="server" Checked='<%# Convert.ToBoolean(Eval("Unlock")) %>' />
    </EditItemTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="chkUnlock" runat="server" Enabled="false" Checked='<%# Convert.ToBoolean(Eval("Unlock")) %>' />
    </ItemTemplate>
</asp:TemplateField>

<asp:CheckBoxField HeaderText="Reset" SortExpression="Reset" DataField="Reset" InsertVisible="false" ReadOnly="false" />

我正在使用内置的 CheckBoxField 和 TemplateField,希望它们中的一个可以工作,但也没有。我只从复选框中获取原始值。例如,如果复选框被选中,并且我在编辑模式下取消选中它然后按更新,代码将其读取为仍处于选中状态。

这是 gridview 的 RowUpdating 事件。

protected void grdManageActionGroups_RowUpdating(object sender, GridViewUpdateEventArgs e)
{           
    Label lblTargetGroupEdit = (Label)grdManageActionGroups.Rows[e.RowIndex].FindControl("lblTargetGroupEdit");

    CheckBox chkEditUnlock = ((CheckBox)grdManageActionGroups.Rows[e.RowIndex].FindControl("chkEditUnlock"));

    database.UpdateTargetGroup(ddlActionGroups.SelectedValue, lblTargetGroupEdit.Text, chkEditUnlock.Checked, (grdManageActionGroups.Rows[e.RowIndex].Cells[2].Controls[0] as CheckBox).Checked);
    FillTargetGroupsInGrid();
}

如果您能提供任何帮助,我将不胜感激。

【问题讨论】:

    标签: c# asp.net gridview checkbox


    【解决方案1】:

    发现问题,

    我需要添加

    if(!isPostBack)
    {
        FillTargetGroupsInGrid();
    }
    

    到 GridView 的加载事件

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多