Asp.net 以编程方式将TextBox控件插入GridView.FooterRow

Asp.net 以编程方式将TextBox控件插入GridView.FooterRow,asp.net,c#-3.0,Asp.net,C# 3.0,您可以修改GridView的HeaderRow中的列和列的项模板。但是,在页脚行上不可能执行相同的操作,因为它是只读的 是否有任何方法可以通过编程方式将文本框控件添加到GridView控件中的页脚行? 注意:我不需要对我添加的控件进行数据绑定。我可能缺少一些内容,但您不能连接到RowDataBound事件并像这样添加控件吗 protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if

您可以修改GridViewHeaderRow中的列和列的项模板。但是,在页脚行上不可能执行相同的操作,因为它是只读的

是否有任何方法可以通过编程方式将文本框控件添加到GridView控件中的页脚行?



注意:我不需要对我添加的控件进行数据绑定。

我可能缺少一些内容,但您不能连接到RowDataBound事件并像这样添加控件吗

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        TextBox textBox = new TextBox();
        textBox.Text = "Hello";
        e.Row.Cells[0].Controls.Add(textBox);
    }
}

我可能遗漏了一些东西,但是您不能挂接到RowDataBound事件并像这样添加控件吗

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        TextBox textBox = new TextBox();
        textBox.Text = "Hello";
        e.Row.Cells[0].Controls.Add(textBox);
    }
}

是的,我自己也试过,效果很好,这是完整的代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"    OnRowDataBound="GridView1_RowDataBound"
            ShowFooter="True">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<string> animals = new List<string>();
        animals.Add("Cat");
        animals.Add("Dog");
        animals.Add("Horse");

        GridView1.DataSource = animals;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Footer) return;
        TextBox textBox = new TextBox();
        e.Row.Cells[0].Controls.Add(textBox);
    }
}

使用制度;
使用System.Collections.Generic;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类\u默认值:第页
{
受保护的无效页面加载(对象发送方、事件参数e)
{
列出动物=新列表();
动物。加上(“猫”);
动物。加上(“狗”);
动物。加上(“马”);
GridView1.DataSource=动物;
GridView1.DataBind();
}
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType!=DataControlRowType.Footer)返回;
TextBox TextBox=新建TextBox();
e、 行。单元格[0]。控件。添加(文本框);
}
}

是的,我自己也尝试过,效果很好,下面是完整的代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"    OnRowDataBound="GridView1_RowDataBound"
            ShowFooter="True">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<string> animals = new List<string>();
        animals.Add("Cat");
        animals.Add("Dog");
        animals.Add("Horse");

        GridView1.DataSource = animals;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Footer) return;
        TextBox textBox = new TextBox();
        e.Row.Cells[0].Controls.Add(textBox);
    }
}

使用制度;
使用System.Collections.Generic;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类\u默认值:第页
{
受保护的无效页面加载(对象发送方、事件参数e)
{
列出动物=新列表();
动物。加上(“猫”);
动物。加上(“狗”);
动物。加上(“马”);
GridView1.DataSource=动物;
GridView1.DataBind();
}
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType!=DataControlRowType.Footer)返回;
TextBox TextBox=新建TextBox();
e、 行。单元格[0]。控件。添加(文本框);
}
}

尝试在
gridview\u RowCreated
事件中向
gridview
添加控件

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {`enter code here`
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            TextBox txt = new TextBox();
            txt.ID = "txt_Name";
            e.Row.Cells[0].Controls.Add(txt);


        }

gridview\u RowCreated
事件中,尝试向
gridview
添加控件

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {`enter code here`
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            TextBox txt = new TextBox();
            txt.ID = "txt_Name";
            e.Row.Cells[0].Controls.Add(txt);


        }

我试过你的建议,但似乎不起作用。我没有得到编译错误。您自己试过了吗?firstAndAntilus是正确的,您需要启用页脚,但这不会导致生成错误。请看下面我的完整答案。我已经尝试了你的建议,但似乎不起作用。我没有得到编译错误。您自己试过了吗?firstAndAntilus是正确的,您需要启用页脚,但这不会导致生成错误。见下面我的完整答案。