我无法从gridview的页脚行获取文本框的值

时间:2013-01-31 08:04:50

标签: c# asp.net gridview

我无法从gridview的页脚行获取文本框的值

    <asp:GridView ID="GridView1" runat="server" Width="1214px"       
            AutoGenerateColumns="False" ShowFooter="true" 
 OnRowCommand="GridView1_RowCommand"                                  
  <Columns>
 <asp:TemplateField HeaderText="Insert">
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Student Name">
            <EditItemTemplate>
               <asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>'/>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtSName" runat="server"/>
            </FooterTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:GridView>

后面的代码是........ 我无法从gridview的页脚行获取文本框的值

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName==("AddNew"))
    {
        TextBox txtName =(TextBox) GridView1.FooterRow.FindControl("txtSName");
string strName=txtName.Text;   //strName is Empty while i m enters data into the  textbox txtSName
     }

2 个答案:

答案 0 :(得分:2)

发表此评论以帮助可能遇到相同问题的其他人。

确保在回发后再次加载网格。

页面加载中的代码应该如下所示

if (!IsPostBack)
{
  LoadGrid();
}

答案 1 :(得分:1)

你的标记正在咬你。你在模板字段中有两个页脚模板。 这样做

<asp:TemplateField HeaderText="Insert">
     <ItemTemplate>
        <asp:ImageButton ID="EditImageButton" runat="server" CommandName="Edit"
            ImageUrl="~/images/Edit.png"Style="height: 16px" ToolTip="Edit" />
    </ItemTemplate>
    <EditItemTemplate>
       <asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" 
            CausesValidation="false" OnClientClick="return confirm('Delete.Are you sure you want to delete?')" 
            ImageUrl="~/images/DeleteTS.png" Text="Cancel" />
    </EditItemTemplate>
    <FooterTemplate>
          <asp:ImageButton ID="AddNewImgBtn" runat="server" ImageUrl="~/images/saveHS.png"
               ToolTip="Add New" AlternateText="Add New" Width="16px" Height="16px" 
               CommandArgument="InsertNew" ImageAlign="AbsMiddle" />
    </FooterTemplate>             
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Student Name">
     <EditItemTemplate>
          <asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>' />
     </EditItemTemplate>
      <ItemTemplate>
            <asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
      </ItemTemplate>
      <FooterTemplate>
          <asp:TextBox ID="txtSName" runat="server"/>
      </FooterTemplate>
      </asp:TemplateField>
  </Columns>
  </asp:GridView>

然后你可以优雅地找到你的页脚行。

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
           if (e.CommandArgument=="InsertNew")
         {
         GridView testGrid=(Gridview)sender;
           TextBox txtName =(TextBox)testGrid.FooterRow.FindControl("txtSName");
        string strName=txtName.Text;   
          }
         }

参见工作示例Example