【问题标题】:CheckBox in ASP.NET GridView template field does not retain it's value on submitASP.NET GridView 模板字段中的 CheckBox 在提交时不保留其值
【发布时间】:2011-06-29 01:51:22
【问题描述】:

我正在尝试在 GridView 的 TemplateField 中使用 CheckBoxes 从该 GridView 中选择多个条目。 GridView 的数据源是在页面加载时生成的项目列表。

<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False" 
            AllowPaging="True" onpageindexchanging="TANsGridView_PageIndexChanging" 
            DataKeyNames="GUID">
            <Columns>
                <asp:TemplateField ShowHeader="False" HeaderText="Checker">
                    <ItemTemplate>
                        <asp:CheckBox ID="SelectCheckbox" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>

问题是,当我按下提交按钮时,所有 CheckBox 都返回 Checked 属性为“false”。

为了循环遍历行,我使用:

foreach (GridViewRow row in TANsGridView.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("SelectCheckbox");
        }

我应该使用什么来获得正确的值?

谢谢,卡塔林

【问题讨论】:

  • 您是否每次都错误地在页面加载时重新绑定网格视图? gridview 绑定代码应该包含在 if 语句中,确保它只在不回发时完成。

标签: asp.net gridview checkbox templatefield


【解决方案1】:

您是否每次都错误地在页面加载时重新绑定网格视图? gridview 绑定代码应该包含在 if 语句中,确保它只在不回发时完成。

我现在应该把它放在这里接受检查吗? :)

【讨论】:

    【解决方案2】:

    如果您在页面加载时绑定网格。像这样加载网格。

    if(!ispostback)
    {
    ..........loading data to databind.
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-29
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多