0

我无法从 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
     }
4

2 回答 2

2

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

确保网格不会在回发时再次加载。

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

if (!IsPostBack)
{
  LoadGrid();
}
于 2014-10-15T10:57:05.180 回答
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;   
          }
         }

请参阅工作示例示例

于 2013-01-31T08:33:35.680 回答