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

标签 c# asp.net

就像标题中一样+如何处理按钮单击 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 block 中,检查 commandname 是否是您所期望的。此事件还用于删除、编辑等,因此如果您不将其包装在其中,您最终可能会运行您不想要的事件的代码。

关于c# - 如何从gridview页脚C#中的文本框中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694213/

相关文章:

asp.net - 为什么我不能调用表单内容?

c# - 是否有用于获取字符串列表中最长字符串的 LINQ 函数?

c# - Eazfuscator.NET 破坏了 Enum.IsDefined

c# - 类 : Fields, 属性、构造函数、方法中项目的顺序

c# - SignalR - 检查用户是否仍然连接

c# - 在 .NET Web 服务中,SoapException.Message 属性如何易于阅读?

C# 泛型约束

asp.net - 是否可以在 ASP.NET webforms 中使用 get 而不是 post?

asp.net - MVC+ Razor : how to add starting <div> conditionally?

c# - 自定义角色提供者不实现继承的抽象成员