我有一个gridview,可以在sqldatasource的帮助下填充。
创建gridview后,我想添加一个创建Notes的手动列,以便用户可以输入每行的注释,然后单击提交表单。
在网格视图的<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;
}
所以字符串注释总是空的,似乎在回发时值被重置;因为它重新创建了txtNotes文本框。
如何保留回发价值?
答案 0 :(得分:3)
绑定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
}