Dynamically added controls are lost on a Postback

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Dynamically added controls are lost on a Postback

    Hi,

    i created a custom control overriden from HierarchicalDataBoundControl. In
    method PerformDataBinding I add some controls (Controls.Add(new TextBox)). It
    works fine on a first load, but on a postback there are no controls.
    I know I have to create them again, but a method PerformDataBinding doesn't
    occour on a PostBack. Can someone tell me where and when I have to create
    them?

    Thanks in advance,
    Dejan
    Dejan Guest

  2. Similar Questions and Discussions

    1. How to retrieve values of dynamically added web controls on the form??
      Hi, I have created a survey form in asp.net that has dynamically generated fields( based on the record that I get from the DB I adding a text box...
    2. user controls: dynamiclly added child controls dont survive post back ?
      hi, i have some strange behaviour: i've created a web user control that add's some child controls (e.g: textbox, image buttons) to its control...
    3. Trouble with dynamically added controls
      Hi everybody, I need to dynamically populate a webpage at runtime with controls. This is the code I wrote. Public Class WebForm1 ...
    4. onclick event for dynamically added controls?
      when you build the control you will need to set it's id to a unique value. then you will need to daisy chain the click event to a handler...
    5. Positioning Dynamically added controls
      Hi I am adding a textbox control at runtime that allows a user to enter a date. I also want to add a calendar icon beside the textbox so that the...
  3. #2

    Default Re: Dynamically added controls are lost on a Postback

    "Dejan" <Dejan@discussions.microsoft.com> wrote in message
    news:AFF127FB-E66C-494F-A433-E56505FBFE8D@microsoft.com...
    > Hi,
    >
    > i created a custom control overriden from HierarchicalDataBoundControl. In
    > method PerformDataBinding I add some controls (Controls.Add(new TextBox)).
    > It
    > works fine on a first load, but on a postback there are no controls.
    > I know I have to create them again, but a method PerformDataBinding
    > doesn't
    > occour on a PostBack. Can someone tell me where and when I have to create
    > them?
    I know how we did this in .NET 1.1. Maybe you can modify the following for
    2.0:

    1) During databinding, something in the data tells you that you need your
    text box. For the sake of discussion, let's assume that the data source has
    a "TextBoxNeeded" column. You should put that value into ViewState. This is
    typically done by setting the data source column value into a control
    property (bool TextBoxNeeded) which sets and gets its value from
    ViewState["TextBoxNeeded"].
    2) During CreateChildControls, you would check that property to see if you
    need to create the textbox. On the initial page request, the property would
    be true because you did the data bind and the data source said that the
    property should be set. On postback, the property would be set from
    ViewState. In either case, you would then create your textbox.

    A variation on this theme are that if the textbox takes any of its
    properties from the data source, then you'll want to create it during the
    data bind and set the properties from the data source. Set a flag indicating
    that you've created it so that you don't create it again during
    CreateChildControls on the initial request. On the postback, when
    CreateChildControls is called, your flag will not have been set, so you
    would then create the text box and add it to the Controls collection. At
    that time, the text box will set its own properties from its own part of
    ViewState.

    If you find that some of this applies to 2.0, please post your results here.
    I haven't written any 2.0 controls yet, and would be interested in the 2.0
    way of doing this.

    John


    John Saunders Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139