Retrieving values from dynamically added controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Sokalski

    Retrieving values from dynamically added controls

    I have a custom control that I wrote (I inherit from
    System.Web.UI.W ebControls.Comp ositeControl). 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
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。



  • Riki

    #2
    Re: Retrieving values from dynamically added controls

    Nathan Sokalski wrote:
    I have a custom control that I wrote (I inherit from
    System.Web.UI.W ebControls.Comp ositeControl). 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


    Comment

    • Eliyahu Goldin

      #3
      Re: Retrieving values from dynamically added controls

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

      Read through this:


      --
      Eliyahu Goldin,
      Software Developer
      Microsoft MVP [ASP.NET]




      "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
      news:e61d2Cp8HH A.536@TK2MSFTNG P06.phx.gbl...
      >I have a custom control that I wrote (I inherit from
      >System.Web.UI. WebControls.Com positeControl). 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
      njsokalski@hotm ail.com
      有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

      >

      Comment

      • Phil H

        #4
        Re: Retrieving values from dynamically added controls

        On 9 Sep, 04:23, "Nathan Sokalski" <njsokal...@hot mail.comwrote:
        I have a custom control that I wrote (I inherit from
        System.Web.UI.W ebControls.Comp ositeControl). 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

        Comment

        • Ashok Kunwar Singh

          #5
          Re: Retrieving values from dynamically added controls

          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



          Comment

          • Scott Roberts

            #6
            Re: Retrieving values from dynamically added controls

            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:




            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.

            Comment

            Working...