如何从文本框这是在GridView的页脚C#值? [英] how to get values from textbox which is in gridview Footer c#?

查看:74
本文介绍了如何从文本框这是在GridView的页脚C#值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题+如何处理按钮单击该按钮在GridView的页脚也?

文件的.aspx看起来这

 < ASP:GridView控件ID =GridView1=服务器
     的AutoGenerateColumns =FALSE
        CELLPADDING =4的DataKeyNames =IDEnableModelValidation =真
        前景色=#333333网格=无
        onrowcancelingedit =GridView1_RowCancelingEdit1
        onrowediting =GridView1_RowEditing1
        onrowupdating =GridView1_RowUpdating1AllowPaging =真
        onrowdeleting =GridView1_RowDeleting
        onpageindexchanging =GridView1_PageIndexChangingHEIGHT =322px
        ShowFooter =真onselectedindexchanged =GridView1_SelectedIndexChanged>
        < AlternatingRowStyle背景色=白前景色=#284775/>
        <柱体和GT;
            < ASP:CommandField中ShowEditButton =真/>
            < ASP:BoundField的数据字段=ID的HeaderText =ID可见=FALSE/>
            < ASP:BoundField的数据字段=名称的HeaderText =名称/>
            < ASP:的TemplateField>
                < FooterTemplate>
                    < ASP:文本框ID =改为txtName=服务器/>
                < / FooterTemplate>
            < / ASP:的TemplateField>
            < ASP:BoundField的数据字段=姓氏的HeaderText =姓氏/>
            < ASP:的TemplateField>
                < FooterTemplate>
                    < ASP:文本框ID =txtLastName=服务器/>
                < / FooterTemplate>
            < / ASP:的TemplateField>
            < ASP:BoundField的数据字段=DriveLic的HeaderText =DriveLicense/>
            < ASP:的TemplateField>
                < FooterTemplate>
                    < ASP:文本框ID =txtDriveLicense=服务器/>
                < / FooterTemplate>
            < / ASP:的TemplateField>
            < ASP:BoundField的数据字段=国家的HeaderText =国家/>
            < ASP:的TemplateField>
                < FooterTemplate>
                    < ASP:文本框ID =txtWoj=服务器/>
                < / FooterTemplate>
            < / ASP:的TemplateField>
            < ASP:CommandField中的ShowDeleteButton =真/>
            < ASP:的TemplateField>
                < FooterTemplate>
                    < ASP:按钮的ID =ButtonOK文本=确认=服务器/>
                < / FooterTemplate>
            < / ASP:的TemplateField>
        < /专栏>


解决方案

在你的 GridView_RowCommand 事件您可以访问 GridView1.FooterRow页脚控制.FindControl 方法。

 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
    如果(e.CommandName.Equals(插入,StringComparison.OrdinalIgnoreCase))
    {
        文本框txtSomeNewValue =((文本框)GridView1.FooterRow.FindControl(txtSomeNewValue));
        字符串theTextValue = txtSomeNewValue.Text;
    }
}

更新:包裹在一个if块中的code,检查是否的CommandName 是您所期望的东西。也用于此事件删除,编辑等,因此,如果你不把它包起来这个,你可能最终运行code一个事件你没有打算。

like in title + how to handle button click which button is in GridView Footer also?

file .aspx seems like this

<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>

解决方案

Inside your GridView_RowCommand Event you can access the footer control by GridView1.FooterRow.FindControl method.

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;
    }
}

Update: wrapped the code in an if block that checks if the commandname is what you were expecting. This event is also used for delete, edit, etc, so you might end up running the code for an event you didnt intend if you dont wrap it in this.

这篇关于如何从文本框这是在GridView的页脚C#值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆