Retrieving values from dynamically added controls

Discussion in 'ASP .Net' started by Nathan Sokalski, Sep 9, 2007.

  1. I have a custom control that I wrote (I inherit from
    System.Web.UI.WebControls.CompositeControl). I dynamically add this control
    to my Page, but I was told that dynamically added controls do not survive
    postback. This seems to be true. However, this seems to make dynamically
    adding controls rather pointless to me, since if you cannot retrieve the
    values of the controls when you perform a postback, why add the controls in
    the firstplace? Can someone help me figure out how to retrieve the property
    values of a dynamically added control? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Sep 9, 2007
    #1
    1. Advertising

  2. Nathan Sokalski

    Riki Guest

    Nathan Sokalski wrote:
    > I have a custom control that I wrote (I inherit from
    > System.Web.UI.WebControls.CompositeControl). I dynamically add this
    > control to my Page, but I was told that dynamically added controls do
    > not survive postback. This seems to be true. However, this seems to
    > make dynamically adding controls rather pointless to me, since if you
    > cannot retrieve the values of the controls when you perform a
    > postback, why add the controls in the firstplace? Can someone help me
    > figure out how to retrieve the property values of a dynamically added
    > control? Thanks.


    They do not survive postback on their own. You have to help them.

    You can do that by re-creating them dynamically after the postback.
    On top of that, you have to make sure that they get the same ID before and
    after the postback.
    If not, they will not respond to any events that they were supposed to
    handle.

    Usually, this means that in the page, you have to keep track of the number
    of controls that were added dynamically.

    --

    Riki
    Riki, Sep 9, 2007
    #2
    1. Advertising

  3. If you re-create dynamic controls in Page_PreInit event, they will get their
    properties populated correctly in Page_Load event.

    Read through this:
    http://msdn2.microsoft.com/en-us/library/ms178472.aspx

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Nathan Sokalski" <> wrote in message
    news:...
    >I have a custom control that I wrote (I inherit from
    >System.Web.UI.WebControls.CompositeControl). I dynamically add this control
    >to my Page, but I was told that dynamically added controls do not survive
    >postback. This seems to be true. However, this seems to make dynamically
    >adding controls rather pointless to me, since if you cannot retrieve the
    >values of the controls when you perform a postback, why add the controls in
    >the firstplace? Can someone help me figure out how to retrieve the property
    >values of a dynamically added control? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Eliyahu Goldin, Sep 9, 2007
    #3
  4. Nathan Sokalski

    NightOwl888 Guest

    On Sep 8, 8:23 pm, "Nathan Sokalski" <> wrote:
    > I have a custom control that I wrote (I inherit from
    > System.Web.UI.WebControls.CompositeControl). I dynamically add this control
    > to my Page, but I was told that dynamically added controls do not survive
    > postback. This seems to be true. However, this seems to make dynamically
    > adding controls rather pointless to me, since if you cannot retrieve the
    > values of the controls when you perform a postback, why add the controls in
    > the firstplace? Can someone help me figure out how to retrieve the property
    > values of a dynamically added control? Thanks.
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/



    I have a page with dynamically added controls that I retrieve the
    values from. Basically, you just put the call to the dynamic control
    creation in the Page_Init event and don't check IsPostBack because you
    always want it to run.

    Populating the controls can be done in the Page_Load event, but in
    this case you will need to check IsPostBack. Then, you can create a
    procedure that can read the values of the controls and load them into
    an object for use by the rest of your code.

    Typically control generation will be done in a loop. So it is in this
    loop you must do 3 things:

    1. Generate a unique name for each dynamic control. This can be done
    by using the index of the loop appended to a constant that can also be
    read later when the control value is required. For example
    "ControlName" + index.
    2. Read the name of the control back after it is added to the page.
    This can be done by using the UniqueID property in ASP.NET 2.0 or the
    ID property in ASP.NET 1.1 or before. Usually you will add the
    controls to an HTML table and dynamically generate the rows and
    columns in the table.
    3. Store the name of the control somewhere for use the next time the
    page is posted. I used ViewState for this.

    The 3rd item is the key really. After generating the controls and
    adding them to the page, I created a string with the UniqueID of each
    control like this example. The string was actually created inside the
    loop, after the loop is finished you need to store the names.

    Dim First as Boolean = True
    Dim strNames as String = ""
    For intCount as Integer = 0 to Whatever.Length
    'Create your table row, table cell, and add the control to the
    page here.
    'Note that you will not be able to get the correct ID until after
    the control, table row, and table cell
    'have all been added to the page.

    'Generate the unique name for the control.
    Dim ctrl as New DropDownList (or other control)
    ctrl.ID = "ControlName" + intCount

    'Read the UniqueID from the control. ASP.NET 2.0 will screw up
    your naming convention
    'if you use child controls or master pages, so UniqueID will
    actually get the name used
    'in the output HTML.
    If First Then
    strNames = ctrl.UniqueID
    First = False
    Else
    strNames += "," + ctrl.UniqueID
    End If
    Next

    When you are done looping, strNames will look something like this.
    "ControlName0,ControlName1,ControlName2"

    This value is then stored under a known ViewState value:

    ViewState("ControlNames") = strNames


    Now, when reading the values of the controls (presumably after the
    page posts back) you will have a way to identify them. You can load
    the names into an array for use in the loop that retrieves them by
    using the Split method to change the string back into an array.

    strNames = ViewState("ControlNames")
    Dim Names() As String = strNames.Split(",")

    Then after you create the array, loop through it using the same zero
    based index in the same order as you used to create the controls.
    Then you can use Page.FindControl(Names(index)) to create an instance
    of the control to use to retrieve the properties of the control. Keep
    in mind that FindControl only works on the current naming container,
    so you may need to analyze your page to find out what the naming
    container of your dynamic controls are.

    It is also very critical that the loop you use to create the controls
    is indexed exactly the same way as the loop you use to retreive the
    values or you will not have a way to retrieve the controls.


    NightOwl888
    NightOwl888, Sep 9, 2007
    #4
  5. Nathan Sokalski

    Phil H Guest

    On 9 Sep, 04:23, "Nathan Sokalski" <> wrote:
    > I have a custom control that I wrote (I inherit from
    > System.Web.UI.WebControls.CompositeControl). I dynamically add this control
    > to my Page, but I was told that dynamically added controls do not survive
    > postback. This seems to be true. However, this seems to make dynamically
    > adding controls rather pointless to me, since if you cannot retrieve the
    > values of the controls when you perform a postback, why add the controls in
    > the firstplace? Can someone help me figure out how to retrieve the property
    > values of a dynamically added control? Thanks.


    I hope I'm not at cross purposes here but I don't think that is true.
    I too have been faced with the problem of creating web server controls
    dynamically and then having to work out how to handle postback events,
    or (in the case of check boxes) how to detect their state.

    For me the answer was to read the Key/Value pairs contained in
    Request.Form

    For this to work it's important to assign a value to the ID field of
    the control when creating it, else it will indeed get lost.

    Admittedly I wasn't working with custom controls but I imagine the
    principle is the same based on inheritance from base classes.

    Phil Hall
    Phil H, Sep 9, 2007
    #5
  6. Create dynamic control each time (Postback or First time) in Page Init
    event to load values for control from viewstate or let you use
    viewstate.Then store all these controls into a object array.

    Now In case of postback in Button Click event, you can loop through
    controls object array you made while creating these controls. Where
    you can get all chabged calues of these controls.

    For example:



    in VB .Net



    For Each ctrl As Object In objCtrls
    If (Not Nothing Is ctrl) Then
    'Expecting all tab controls will have save button
    inside them.
    ctrl.Save()
    End If
    Next



    Hope it will help you, Lemme know if you need clarifications.

    Regards,
    AKS
    http://www.aksClassifieds.com
    http://forums.aksClassifieds.com
    Ashok Kunwar Singh, Oct 10, 2007
    #6
  7. Nathan Sokalski

    Beatrix Guest

    Hi! Can anyone please explain this for a textbox.text value? After I click a
    button, all the controls disappear, and also the textboxes and their values,
    which I need in the next process.

    Thank you.

    "NightOwl888" wrote:

    > On Sep 8, 8:23 pm, "Nathan Sokalski" <> wrote:
    > > I have a custom control that I wrote (I inherit from
    > > System.Web.UI.WebControls.CompositeControl). I dynamically add this control
    > > to my Page, but I was told that dynamically added controls do not survive
    > > postback. This seems to be true. However, this seems to make dynamically
    > > adding controls rather pointless to me, since if you cannot retrieve the
    > > values of the controls when you perform a postback, why add the controls in
    > > the firstplace? Can someone help me figure out how to retrieve the property
    > > values of a dynamically added control? Thanks.
    > > --
    > > Nathan Sokalski
    > > ://www.nathansokalski.com/

    >
    >
    > I have a page with dynamically added controls that I retrieve the
    > values from. Basically, you just put the call to the dynamic control
    > creation in the Page_Init event and don't check IsPostBack because you
    > always want it to run.
    >
    > Populating the controls can be done in the Page_Load event, but in
    > this case you will need to check IsPostBack. Then, you can create a
    > procedure that can read the values of the controls and load them into
    > an object for use by the rest of your code.
    >
    > Typically control generation will be done in a loop. So it is in this
    > loop you must do 3 things:
    >
    > 1. Generate a unique name for each dynamic control. This can be done
    > by using the index of the loop appended to a constant that can also be
    > read later when the control value is required. For example
    > "ControlName" + index.
    > 2. Read the name of the control back after it is added to the page.
    > This can be done by using the UniqueID property in ASP.NET 2.0 or the
    > ID property in ASP.NET 1.1 or before. Usually you will add the
    > controls to an HTML table and dynamically generate the rows and
    > columns in the table.
    > 3. Store the name of the control somewhere for use the next time the
    > page is posted. I used ViewState for this.
    >
    > The 3rd item is the key really. After generating the controls and
    > adding them to the page, I created a string with the UniqueID of each
    > control like this example. The string was actually created inside the
    > loop, after the loop is finished you need to store the names.
    >
    > Dim First as Boolean = True
    > Dim strNames as String = ""
    > For intCount as Integer = 0 to Whatever.Length
    > 'Create your table row, table cell, and add the control to the
    > page here.
    > 'Note that you will not be able to get the correct ID until after
    > the control, table row, and table cell
    > 'have all been added to the page.
    >
    > 'Generate the unique name for the control.
    > Dim ctrl as New DropDownList (or other control)
    > ctrl.ID = "ControlName" + intCount
    >
    > 'Read the UniqueID from the control. ASP.NET 2.0 will screw up
    > your naming convention
    > 'if you use child controls or master pages, so UniqueID will
    > actually get the name used
    > 'in the output HTML.
    > If First Then
    > strNames = ctrl.UniqueID
    > First = False
    > Else
    > strNames += "," + ctrl.UniqueID
    > End If
    > Next
    >
    > When you are done looping, strNames will look something like this.
    > "ControlName0,ControlName1,ControlName2"
    >
    > This value is then stored under a known ViewState value:
    >
    > ViewState("ControlNames") = strNames
    >
    >
    > Now, when reading the values of the controls (presumably after the
    > page posts back) you will have a way to identify them. You can load
    > the names into an array for use in the loop that retrieves them by
    > using the Split method to change the string back into an array.
    >
    > strNames = ViewState("ControlNames")
    > Dim Names() As String = strNames.Split(",")
    >
    > Then after you create the array, loop through it using the same zero
    > based index in the same order as you used to create the controls.
    > Then you can use Page.FindControl(Names(index)) to create an instance
    > of the control to use to retrieve the properties of the control. Keep
    > in mind that FindControl only works on the current naming container,
    > so you may need to analyze your page to find out what the naming
    > container of your dynamic controls are.
    >
    > It is also very critical that the loop you use to create the controls
    > is indexed exactly the same way as the loop you use to retreive the
    > values or you will not have a way to retrieve the controls.
    >
    >
    > NightOwl888
    >
    >
    Beatrix, Nov 20, 2007
    #7
  8. As the previous poster mentioned, the short of it is, you need to create the
    dynamic controls in the Page_Init event. You also need to be sure you give
    them the same ID (I believe). If you do that, then later in the page
    lifecycle (like in Page_Load or any other events) the control will contain
    its postback value.

    If you're going to add controls to a page dynamically, it would really be a
    good idea to get a firm grasp on the page life-cycle - how controls are
    created in Page_Init, how they are populated with postback values, etc.
    Actually, it's a good idea to know that stuff even if you don't use dynamic
    controls.

    There are some pretty good articles out there on the "how's" and "why's" of
    dynamic controls. Here's a start:

    http://aspnet.4guysfromrolla.com/articles/092904-1.aspx



    > Hi! Can anyone please explain this for a textbox.text value? After I click
    > a
    > button, all the controls disappear, and also the textboxes and their
    > values,
    > which I need in the next process.
    Scott Roberts, Nov 20, 2007
    #8
    1. Advertising

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. Jeffrey Todd
    Replies:
    1
    Views:
    4,078
    Peter Blum
    Jun 2, 2005
  2. =?Utf-8?B?ZG90bmV0dGVzdGVy?=

    Accessing values of dynamically added controls on Postback

    =?Utf-8?B?ZG90bmV0dGVzdGVy?=, Jul 14, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    462
    Eliyahu Goldin
    Jul 14, 2005
  3. Replies:
    0
    Views:
    1,663
  4. Nathan Sokalski

    Retrieving values from dynamically added controls

    Nathan Sokalski, Sep 9, 2007, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    933
    Beatrix
    Nov 20, 2007
  5. Nathan Sokalski

    Retrieving values from dynamically added controls

    Nathan Sokalski, Sep 9, 2007, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    226
    Scott Roberts
    Nov 20, 2007
Loading...

Share This Page