当我尝试向现有网格添加新行时,我的值正在被清除。使用 Viewstate 我维护数据表彻底页面并将其绑定到网格。认为这些值在回发期间被清除。
// 添加新行
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
int i;
if (dtCurrentTable.Rows.Count > 0)
{
for (i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox txtguide = (TextBox)grdreports.Rows[rowIndex].FindControl("txtAccNo");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["txtAccNo"] = txtguide.Text;
rowIndex += 1;
}
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
ViewState["rowcount"] = dtCurrentTable.Rows.Count;
grdreports.DataSource = dtCurrentTable;
grdreports.DataBind();
}
}
// aspx 页面:
<asp:GridView ID="grdreports" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
S.No</HeaderTemplate>
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Acc.No/Tel.No">
<ItemTemplate>
<asp:TextBox ID="txtAccNo" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "nvrGuide")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AddNew" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="lbtnAddGuide" runat="server" CommandName="Add" Text="Add New"
OnClick="lbtnAddGuide_OnClick"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteGuide" runat="server" CommandName="Delete" Text="Delete"
OnClick="lbtnDeleteGuide_OnClick"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我哪里错了?