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

时间:2011-04-17 14:57:27

标签: c# asp.net

如标题+如何处理按钮单击哪个按钮在GridView Footer中呢?

文件.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>

1 个答案:

答案 0 :(得分:4)

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是否符合您的预期。此事件也用于删除,编辑等,因此如果您不将其包装在此中,您最终可能会运行您不想要的事件的代码。