如何从gridview Footer c#中的文本框中获取值

本文关键字:文本 获取 gridview Footer | 更新日期: 2023-09-27 17:50:19

喜欢在标题+如何处理按钮点击哪个按钮是在GridView页脚也?

file .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 Footer c#中的文本框中获取值

在您的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是否是您所期望的。这个事件也用于删除,编辑等,所以如果你不把它包装在This中,你可能最终会运行一个你不想要的事件的代码。