微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从gridview页脚c#中的文本框获取值?

如何解决如何从gridview页脚c#中的文本框获取值?

| 就像标题+如何处理按钮一样,单击GridView页脚中的哪个按钮也是? 文件.aspx看起来像这样
<asp:GridView ID=\"GridView1\" runat=\"server\"
     AutoGenerateColumns=\"False\" 
        CellPadding=\"4\" DataKeyNames=\"id\" EnableModelValidation=\"True\" 
        ForeColor=\"#333333\" GridLines=\"None\" 
        onrowcancelingedit=\"GridView1_RowCancelingEdit1\" 
        onrowediting=\"GridView1_RowEditing1\" 
        onrowupdating=\"GridView1_RowUpdating1\" AllowPaging=\"True\" 
        onrowdeleting=\"GridView1_RowDeleting\" 
        onpageindexchanging=\"GridView1_PageIndexChanging\" Height=\"322px\" 
        ShowFooter=\"True\" onselectedindexchanged=\"GridView1_SelectedindexChanged\" >
        <AlternatingRowStyle BackColor=\"White\" ForeColor=\"#284775\" />
        <Columns>
            <asp:CommandField ShowEditButton=\"True\" />
            <asp:BoundField datafield=\"id\" HeaderText=\"ID\" Visible=\"False\" />
            <asp:BoundField datafield=\"Name\" HeaderText=\"Name\" />
            <asp:TemplateField>
                <FooterTemplate>
                    <asp:TextBox ID=\"txtName\" runat=\"server\" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:BoundField datafield=\"LastName\" HeaderText=\"LastName\" />
            <asp:TemplateField>
                <FooterTemplate>
                    <asp:TextBox ID=\"txtLastName\" runat=\"server\" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:BoundField datafield=\"DriveLic\" HeaderText=\"DriveLicense\" />
            <asp:TemplateField>
                <FooterTemplate>
                    <asp:TextBox ID=\"txtDriveLicense\" runat=\"server\" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:BoundField datafield=\"country\" HeaderText=\"Country\" />
            <asp:TemplateField>
                <FooterTemplate>
                    <asp:TextBox ID=\"txtWoj\" runat=\"server\" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowDeleteButton=\"True\" />
            <asp:TemplateField>
                <FooterTemplate>
                    <asp:Button ID=\"ButtonOK\" Text=\"Confirm\" runat=\"server\" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    

解决方法

GridView_RowCommand
事件中,您可以通过
GridView1.FooterRow.FindControl
方法访问页脚控件。
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals(\"Insert\",StringComparison.OrdinalIgnoreCase))
    {
        TextBox txtSomeNewValue = ((TextBox)GridView1.FooterRow.FindControl(\"txtSomeNewValue\"));
        string theTextValue = txtSomeNewValue.Text;
    }
}
更新:将代码包装在if块中,以检查
commandname
是否符合您的期望。此事件还用于删除,编辑等,因此,如果不将其包装在其中,可能最终会运行该事件的代码。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。