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

时间:2009-01-29 11:03:15

标签: asp.net c#-3.0

您可以在 GridView HeaderRow 中修改列的列和 ItemTemplate 。但是 FooterRow 是不可能的,因为它是只读的。

有什么方法可以编程方式将 TextBox 控件添加到 GridView 控件中的FooterRow?


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

3 个答案:

答案 0 :(得分:1)

我可能遗漏了一些东西,但你不能挂钩到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);
    }
}

答案 1 :(得分: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);
    }
}

答案 2 :(得分:0)

尝试在gridview事件

中向gridview_RowCreated添加控件
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);


        }