【发布时间】:2010-10-27 08:45:35
【问题描述】:
我有一个包含以下列的 GridView
<asp:TemplateField HeaderText="Name">
<FooterTemplate>
<asp:TextBox ID="txt_Name" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lbl_name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "t_Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_name" runat="server" Width="100px" Text='<%#DataBinder.Eval(Container.DataItem,"t_Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created By">
<ItemTemplate>
<asp:Label ID="lbl_tabcreatedby" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "t_CreatedBy") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Modify" ShowEditButton="True" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
<asp:TemplateField HeaderText="Add a New Name">
<FooterTemplate>
<asp:LinkButton ID="lnkbtn_AddName" runat="server" CommandName="Insert">Add Name</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
然后在后面的代码中,我试图以
的身份访问 txt_Name 文本框protected void gv_Name_RowCommand(object sender, GridViewCommandEventArgs e)
{
string t_Name = ((TextBox)(gv_Name.FooterRow.FindControl("txt_Name"))).Text;
// Insert Code
}
但是无论 txt_Name 的当前文本是什么,我每次都在字符串 t_Name 中得到空值。 但是,如果我禁用页面的 ViewState,我可以获得文本。任何解释。
【问题讨论】:
-
尝试检查您是否获得了文本框控件?就这一行: ((TextBox)(gv_Name.FooterRow.FindControl("txt_Name")))
-
是的,我正在获取 TextBox,但 Text 为空。