DataList & Dynamic Controls

Discussion in 'ASP .Net' started by Scott Schluer, Feb 8, 2004.

  1. 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
     
    Scott Schluer, Feb 8, 2004
    #1
    1. Advertisements

  2. Scott Schluer

    Teemu Keiski Guest

    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" <> wrote in message
    news:%...
    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
     
    Teemu Keiski, Feb 8, 2004
    #2
    1. Advertisements

  3. Scott Schluer

    meenugoel

    Joined:
    Feb 27, 2009
    Messages:
    1
    Likes Received:
    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.


     
    meenugoel, May 27, 2014
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick

    Datalist adding Dynamic controls

    Patrick, Jan 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,864
    Patrick
    Jan 28, 2004
  2. =?Utf-8?B?VGVyb3M=?=

    Dynamic Variables? OR Dynamic Controls

    =?Utf-8?B?VGVyb3M=?=, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    642
    Scott Allen
    Aug 10, 2004
  3. Nevyn Twyll
    Replies:
    8
    Views:
    2,385
  4. Replies:
    5
    Views:
    5,860
  5. Ronald
    Replies:
    2
    Views:
    1,111
    Robbe Morris [C# MVP]
    Jan 17, 2006
  6. =?Utf-8?B?U3RldmUgRnVuaw==?=

    Dynamic Wizard with Dynamic Validation Controls

    =?Utf-8?B?U3RldmUgRnVuaw==?=, Oct 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    599
    =?Utf-8?B?U3RldmUgRnVuaw==?=
    Oct 13, 2006
  7. TheDude5B
    Replies:
    0
    Views:
    671
    TheDude5B
    Feb 28, 2007
  8. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    706
    Eidolon
    May 4, 2004
Loading...