【问题标题】:asp:gridview dynamically created columnsasp:gridview 动态创建的列
【发布时间】:2014-03-19 13:27:04
【问题描述】:

我有一个在 sqldatasource 的帮助下填充的 gridview。

创建网格视图后,我想添加一个手动创建的列注释,以便用户可以为每一行输入注释,然后单击提交表单。

在网格视图的<Columns> 字段中,我有这个:

<asp:TemplateField HeaderText="Notes">
                    <ItemTemplate>
                        <asp:TextBox ID="txtNotes" runat="server"></asp:TextBox>
                    </ItemTemplate>
</asp:TemplateField>

这实际上可以很好地创建新列,但是当我在文本框中输入数据时,单击提交时它会消失。 在提交中,即使我像这样遍历每个 gridview 行:

foreach (GridViewRow row in gvResults.Rows)
{ 
   row.Cells[0].Text = 
   ...
   string notes = (row.Cells[10].FindControl("txtNotes") as TextBox).Text;
}

所以字符串 notes 始终为空,似乎在回发时该值被重置;因为它重新创建了 txtNotes 文本框。

我怎样才能保持回发的价值?

【问题讨论】:

  • 您在行中迭代哪个页面事件?
  • 你是否在回发时绑定网格?
  • 遍历此事件:protected void btnSubmit_Click(object sender, EventArgs e)

标签: c# asp.net gridview


【解决方案1】:

当您绑定 GridView 时,请务必检查它是否为回发。

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        LoadData(); //if you did this without checking IsPostBack, you'd rebind the GridView and lose the existing rows
    }

protected void LoadData()
{
    //bind GridView etc
}

【讨论】:

  • 抱歉标签应该是文本框,只是一个错字,它是代码中的文本框,是的,我在回发时重新绑定,只是注意到
猜你喜欢
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 2012-09-20
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多