2013-07-22 29 views
2

针对以下问题发布的解决方案似乎对我无效。如何从C#的gridview的页脚中的控件获取值?

how to get values from textbox which is in gridview Footer c#?

这里是我的网格。

 <asp:GridView ID="gridOccupants" runat="server" AllowPaging="True" AutoGenerateColumns="false" 
     OnRowCommand="gridOccupants_RowCommand"> 
     <asp:TemplateField HeaderText="Name" ItemStyle-Width="15px" FooterStyle-Width="15px"> 
      <asp:TemplateField HeaderText="Id" ItemStyle-Width="15px" FooterStyle-Width="15px"> 
       <itemtemplate> 
       <asp:Label ID="lblid" runat="server" Text='<%#Bind("id")%>'></asp:Label> 
       </itemtemplate> 
       <footertemplate> 
       <asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" OnClick="InsertOccupant_Click" 
            UseSubmitBehavior="False" /> 
       </footertemplate> 
      </asp:TemplateField> 
      <asp:TemplateField> 
       <itemtemplate> 
      <asp:Label ID="lblName" runat="server" Text='<%#Bind("name")%>'></asp:Label> 
      </itemtemplate> 
       <footertemplate> 
      <asp:TextBox ID="txtname2" runat="server" Width="50px" > </asp:TextBox> 
      </footertemplate> 
      </asp:TemplateField> 
    </asp:GridView> 

    protected void InsertOccupant_Click(object sender, EventArgs e) 
    { 

      GridViewRow row = gridOccupants.FooterRow; 
      string name = ((TextBox)row.FindControl("txtname2")).Text; 
    } 

此代码不会拉出用户在文本框中输入的值 - txtname2。

欢迎任何想法。

+0

什么代码呢?生成异常?为'name'返回空字符串?其他? –

回答

2

这可能是因为您正在处理按钮的onclick事件,而不是像您链接的样本一样处理rowcommand

试试这个:

<asp:GridView ID="gridOccupants" runat="server" AllowPaging="True" AutoGenerateColumns="false" 
    OnRowCommand="gridOccupants_RowCommand"> 
    <asp:TemplateField HeaderText="Name" ItemStyle-Width="15px" FooterStyle-Width="15px"> 
     <asp:TemplateField HeaderText="Id" ItemStyle-Width="15px" FooterStyle-Width="15px"> 
      <itemtemplate> 
      <asp:Label ID="lblid" runat="server" Text='<%#Bind("id")%>'></asp:Label> 
      </itemtemplate> 
      <footertemplate> 
      <asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="False" /> 
      </footertemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
      <itemtemplate> 
     <asp:Label ID="lblName" runat="server" Text='<%#Bind("name")%>'></asp:Label> 
     </itemtemplate> 
      <footertemplate> 
     <asp:TextBox ID="txtname2" runat="server" Width="50px" > </asp:TextBox> 
     </footertemplate> 
     </asp:TemplateField> 
</asp:GridView> 

protected void gridOccupants_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName.Equals("Insert", StringComparison.OrdinalIgnoreCase)) 
    { 
     GridViewRow row = ((GridView)sender).FooterRow; 
     TextBox txtname2 = (TextBox)row.FindControl("txtname2"); 
     if (txtname2 == null) 
     { 
      return; 
     } 
     string name = txtname2.Text; 
    } 
} 

关闭我的头顶,我不知道哪里是UseSubmitBehavior="False"有来自于。如果我的例子不起作用,那么你可能需要删除它。

+0

这对我来说是唯一的额外代码:<%@ Page EnableEventValidation =“false”%>。谢谢rtpHarry。 –

0

试试这个:

protected void gridOccupants_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Insert") 
    { 
     var txtFName1 = (TextBox)((GridView)sender).FooterRow.FindControl("txtname2"); 
     if (txtname2 == null) 
     { 
      return; 
     } 
     string name = txtname2.Text; 
    } 
} 
+0

你能解释一下你的解决方案吗? –

相关问题