DataGrid in a placeholder control

M

Mark

I've created a datagrid programatically, and added it a placeholder control.
The datagrid contains an edit button for each record. I need to capture
this event some how. However, seeing as the placeholder control appears to
nuke its contents (the DataGrid) on postback, I can't seem to capture
anything. I'm under the assumption that for an event to be raised like
(MyDataGrid_SelectedIndexChanged) to fire, that the placeholder is going to
have to maintain its contents between postbacks - perhaps I'm wrong?

Any suggestions on how to make this work assuming:

1. The datagrid must be created programmatically and added to the
placeholder the first time this page is loaded.

Thanks in advance!

Mark
 
T

Teemu Keiski

Hi,

yes you need to add the DataGrid to the PlaceHolders Controls on postback as
well. And for state management reasons you should do it so that add it to
the PlaceHolder before databinding, as control lifecycle including state
tracking, loading and saving etc are executed after control is added to the
control collection.

So in pseudo-code (in Page_Load):
==
//Create dg columns etc, then add dg to the PlaceHolder
PlaceHolder1.Controls.Add(DataGrid1);

if(!IsPostBack)
{
//Bind the Grid
}
==
This way DataGrid gets added to the Control collection before anything is
done with it. Working with dynamical controls makes sense and is very
similar to statical controls if you think that static controls
(declaratively specified in aspx) exist already in the Controls collection
when you work with them in code, so with dynamical controls the first thing
you should do is to get the controls to the Controls collection. After they
are there, they work exactly as any other controls

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist



I've created a datagrid programatically, and added it a placeholder control.
The datagrid contains an edit button for each record. I need to capture
this event some how. However, seeing as the placeholder control appears to
nuke its contents (the DataGrid) on postback, I can't seem to capture
anything. I'm under the assumption that for an event to be raised like
(MyDataGrid_SelectedIndexChanged) to fire, that the placeholder is going to
have to maintain its contents between postbacks - perhaps I'm wrong?

Any suggestions on how to make this work assuming:

1. The datagrid must be created programmatically and added to the
placeholder the first time this page is loaded.

Thanks in advance!

Mark
 
G

Guest

Dynamically created controls need to be dynamically created on every request, not just the first request (assuming you *want* them to appear on every request). I recommend you do this in OnInit(). Attach to the grid event there, too. If you dont want to worry about recreating controls all the time, consider creating a DynamicPlaceHolder control that stores the type of controls it contains in its viewstate, and automatically creates them each request. There are people out there who have done this, you should be able to find an example

Personally I'd rather recreate the controls myself. ViewState gets large enough as it is...

-Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top