Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > InstantiateIn doesn't execute without .DataBind

Thread Tools

InstantiateIn doesn't execute without .DataBind

Oleg Ogurok
Posts: n/a
Hi all,

I'm adding several TemplateColumns to my grid programmatically in Page_Load
because I don't know how many I need at compile time. Inside each
TemplateColumn, I set ItemTemplate to a class derived from ITemplate. There,
I add a couple of TextBoxes inside Itemplate.InstantiateIn() method.

It seems InstantiateIn() method executes only if I do .DataSource and
..DataBind() on my DataGrid. However, I don't want to rebind the date on
postbacks because it's not necessary. I understand that as long as I
reconstruct my controls, the ViewState data will be automatically restored.
This works well on the columns I define inside aspx.

Is there a way to make InstantiateIn() execute or should I rebuild my
columns and their controls in some other method?
Here's my code:

private void Page_Load(object sender, System.EventArgs e)


DataTable dtPeriods = dsPeriods.Tables["Periods"];

foreach (DataRow row in dtPeriods.Rows)


PeriodColumn periodColumn = new



if (!IsPostBack)

{ dgForecast.DataSource = ds;




class PeriodColumn : TemplateColumn


public PeriodColumn(int periodNo)


this.periodNo = periodNo;

HeaderText = periodNo.ToString();

this.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;

ItemTemplate = new PeriodItemTemplate(periodNo);


private int periodNo;


class PeriodItemTemplate : ITemplate


public PeriodItemTemplate(int periodNo)

{ this.periodNo = periodNo;


#region ITemplate Members

public void InstantiateIn(Control container)


tbCost = new TextBox();

tbCost.Columns = 10;

TextBox tbRevenue = new TextBox();

tbRevenue.ID = "tbRevenue_" + periodNo;

tbRevenue.Columns = 10;

LiteralControl lcCost = new LiteralControl("Cost: ");

LiteralControl lcRevenue = new LiteralControl("Rev: ");

LiteralControl lcSeparator = new LiteralControl("<br>");








Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can i execute aspx file in my asp pag with server.execute method(sorry) Savas Ates ASP General 1 08-17-2004 04:52 PM
execute without a button pressed? cgia ASP .Net 2 04-02-2004 10:50 PM
execute the statements inside if statement without testing the if expression? Matt Java 3 03-01-2004 08:57 PM
Execute server code without complete page refresh (XMLHTTP) Matt ASP .Net 3 01-14-2004 09:50 PM
How must you do to execute PDM without influencing to active PIX? john Cisco 2 12-11-2003 02:37 AM