我有一个gridview,页脚中有两个文本框。所需的是获取文本框值,将其存储到数据表中,然后将其绑定到网格视图。我无法获取文本框值。它们显示为空(如您所见)。我哪里错了。
ASPX:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"
ShowFooter="true" OnRowDataBound="gv_RowDataBound"
OnRowCommand="gv_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit">
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update"
CommandName="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel"
CommandName="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%#Container.DataItemIndex %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbId" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtId" runat="server" Text='<%#Eval("id") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewId" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtNewId"
SetFocusOnError="true"
ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lbName" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#Eval("name") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtNewName" SetFocusOnError="true"
ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete"
CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="Insert"
CommandName="Insert" ></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS:
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
DataTable dt = new DataTable();
switch (e.CommandName)
{
case "Insert":
GridViewRow fRow = gv.FooterRow;
dt.Columns.Add("id");
dt.Columns.Add("name");
dt = (DataTable)ViewState["students"];
DataRow dr = dt.NewRow();
dr["id"] = ((TextBox)fRow.FindControl("txtNewId")).Text;
dr["name"] = ((TextBox)fRow.FindControl("txtNewName")).Text;
dt.Rows.Add(dr);
ViewState["students"] = dt;
gv.DataSource = ViewState["students"];
gv.DataBind();
break;
}
}
文本框是 txtNewId、txtNewName。