tags:

views:

3485

answers:

2

You can modify the columns and the ItemTemplate of columns in a GridView's HeaderRow. But the same is not possible on the FooterRow since it's read-only.

Is there any way that I can programmatically add a TextBox control to a FooterRow in a GridView control?


Note: I don't need databinding on the controls I add.

+1  A: 

I may be missing something but can't you hook into the RowDataBound event and add your controls like this?

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);
    }
}
Chris Needham
I've tried what you've suggested, but it doesn't seem to work. I get no compile-errors. Have you tried this yourself?
roosteronacid
firstAndAntilus is correct that you need to enable the footer, but this shouldn't cause a build error. See my full answer below.
Chris Needham
A: 

Yes I have tried this myself and it works fine, here's the full code...

<%@ 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"&gt;

<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);
    }
}
Chris Needham