468,248 Members | 1,463 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,248 developers. It's quick & easy.

DataList & Dynamic Controls

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

Nov 18 '05 #1
1 2439
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
"Scott Schluer" <us****@webservicesinc.biz> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Yan Wang | last post: by
2 posts views Thread by Patrick | last post: by
3 posts views Thread by gowens | last post: by
3 posts views Thread by mbailey | last post: by
4 posts views Thread by .Net Sports | last post: by
reply views Thread by =?Utf-8?B?Y2luZHk=?= | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.