【问题标题】:Gridview FooterRow textbox text is null when accessing from code behind从后面的代码访问时,Gridview FooterRow 文本框文本为空
【发布时间】: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 为空。

标签: c# .net asp.net gridview


【解决方案1】:

我通过使用附加变量解决了这个问题,请参见以下内容:

Dim txtBox As TextBox = GridView1.FooterRow.FindControl("txtName")
Dim name As String = txtBox.Text

【讨论】:

    【解决方案2】:

    或者您可以尝试按列索引获取文本框,如下所示:

    protected void gv_Name_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string t_Name = ((TextBox)(gv_Name.FooterRow.Cells[5].FindControl("txt_Name"))).Text;
        // Insert Code
    }
    

    【讨论】:

    • 只是为了确保一切正常,检查页面的 ViewState 是否启用。
    • 还有一件事,您必须在页面加载事件中为 GridView 分配一些数据,确保您在 if(!Page.IsPostback) { } 条件内分配数据源。
    • :s 禁用 ViewState 后如何获取控件的状态?
    • 我也很惊讶。我在页面指令中尝试了 EnableViewState="false",现在我可以获取文本框的值
    • 和页面上的其他控件在回发后工作正常吗?
    【解决方案3】:

    在 gv_Name_RowCommand 事件中尝试以下代码

    if (e.CommandName.Equals("Insert"))
     {
    string t_Name = ((TextBox)(gv_Name.FooterRow.FindControl("txt_Name"))).Text; 
    } 
    

    这应该可以工作

    【讨论】:

      【解决方案4】:

      我认为您的数据网格在回发时与数据源断开连接。如果您确定来自 db 的数据/数据值不为空。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-03
        • 1970-01-01
        • 2011-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多