Click to See Complete Forum and Search --> : FindControl function not working with dynamic programmming


AviLaviad
November 15th, 2004, 09:52 AM
there's big mistake for me :(
i tried now to make templateColumn dynamically,
everything was good and the databinding is work well but the FindControl suddenly stopped to work :(

what i did is this:
this is my columnTemplate implementation

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;

public class EditItemTemplate : ITemplate
{
string columnName;

public EditItemTemplate(string colname)
{
columnName = colname;
}

virtual public void InstantiateIn(System.Web.UI.Control container)
{
TextBox tb = new TextBox();
SetProperties(ref tb);

tb.Text = "";
tb.DataBinding += new EventHandler(DataBinding);
tb.Attributes.Add("ID","Name"); // IMPORTANT - set att' name
container.Controls.Add(tb);
}

private void DataBinding(object sender, System.EventArgs e)
{
TextBox lc;

lc = (TextBox) sender;
DataGridItem container = (DataGridItem) lc.NamingContainer;
lc.Text += DataBinder.Eval(container.DataItem, columnName);
}
}


and this is my aspx.cs implementation:

TemplateColumn tc = new TemplateColumn();
tc = new TemplateColumn();
tc.EditItemTemplate = new EditItemTemplate("Name");

dataGrid1.Columns.Add(tc);
dataGrid1.DataBind();


but when i do this:

TextBox text = (TextBox)e.Item.FindControl("Name");


it doesn't work and i get NULL... what's wrong?!

Avi.