DataList & Dynamic Controls

S

Scott Schluer

Hello,

I've got myself a small problem and I'm hoping someone can help.

I have a DataList called dlProducts (displays products from a database).
Within the <ItemTemplate> container of the DataList, I have a PlaceHolder
control called phVariations. During the ItemDataBound event for dlProducts,
I dynamically create a few DropDownLists (again, from a database) and .Add
the DropDownList to the PlaceHolder control. That all works just fine, my
drop downs show up, populated with data, everything looks good.

My problem comes from trying to access this data after postback. The
datalist is only bound when Page.IsPostBack = False and the dynamic controls
are created in the ItemDataBound event. I understand that viewstate only
captures the state of my dynamic controls and not the controls themselves. I
also know I have to recreate my controls during Page_Init or Page_Load and
they will then be repopulated by viewstate.

My question is that since the controls were created during ItemDataBound,
how am I going to recreate them for each Item in the DataList on postback
since the datalist is not bound then? I've been running into a brick wall
with this for a few hours now. Time to turn to others for assistance!

Thanks for anything you can contribute.

Scott
 
T

Teemu Keiski

Hi,

basically you should create the controls in ItemCreated so that they would
become created on every request in this case. ItemCreated happens despite is
it the initial request or postback and it happens before ItemDataBound.

And so if you create them based on data, you need to check in ItemCreated
(say by checking is e.Item.DataItem null or is DataList's DataSource null)
is it the databound recreate action or rehydrate/restoring action as it
happen on every request. If it is databound request, do the stuff in
ItemCreated as you now do, and on other case, just recreate the controls
(you might need to store counts etc into ViewState to be able to recreate
controls). Control state is then restored automatically after you get the
controls recreated.

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


Hello,

I've got myself a small problem and I'm hoping someone can help.

I have a DataList called dlProducts (displays products from a database).
Within the <ItemTemplate> container of the DataList, I have a PlaceHolder
control called phVariations. During the ItemDataBound event for dlProducts,
I dynamically create a few DropDownLists (again, from a database) and .Add
the DropDownList to the PlaceHolder control. That all works just fine, my
drop downs show up, populated with data, everything looks good.

My problem comes from trying to access this data after postback. The
datalist is only bound when Page.IsPostBack = False and the dynamic controls
are created in the ItemDataBound event. I understand that viewstate only
captures the state of my dynamic controls and not the controls themselves. I
also know I have to recreate my controls during Page_Init or Page_Load and
they will then be repopulated by viewstate.

My question is that since the controls were created during ItemDataBound,
how am I going to recreate them for each Item in the DataList on postback
since the datalist is not bound then? I've been running into a brick wall
with this for a few hours now. Time to turn to others for assistance!

Thanks for anything you can contribute.

Scott
 
Joined
Feb 27, 2009
Messages
1
Reaction score
0
Hi,

can you please share the code to add dynamic controls to datalist.
i need to do the same but could not do.

thanks.


Hello,

I've got myself a small problem and I'm hoping someone can help.

I have a DataList called dlProducts (displays products from a database).
Within the <ItemTemplate> container of the DataList, I have a PlaceHolder
control called phVariations. During the ItemDataBound event for dlProducts,
I dynamically create a few DropDownLists (again, from a database) and .Add
the DropDownList to the PlaceHolder control. That all works just fine, my
drop downs show up, populated with data, everything looks good.

My problem comes from trying to access this data after postback. The
datalist is only bound when Page.IsPostBack = False and the dynamic controls
are created in the ItemDataBound event. I understand that viewstate only
captures the state of my dynamic controls and not the controls themselves. I
also know I have to recreate my controls during Page_Init or Page_Load and
they will then be repopulated by viewstate.

My question is that since the controls were created during ItemDataBound,
how am I going to recreate them for each Item in the DataList on postback
since the datalist is not bound then? I've been running into a brick wall
with this for a few hours now. Time to turn to others for assistance!

Thanks for anything you can contribute.

Scott
 

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

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top