Composite Web Control and Dynamically Created Children Controls.

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

  1. #1

    Default Composite Web Control and Dynamically Created Children Controls.

    Hi

    I am developing a web control which creates other web controls. During the
    OnInit of the my web control I read an Xml file which tells my web control
    which child control to create, adding the child controls into a private
    collection.

    In the CreateChildControls (override) method, I loop through my collection
    of child controls, adding them to the web control:
    this.Controls.Add(this.Page.ParseControl(childCont rolCol[i]));

    I do not expose any properties of child controls in the web control.

    Everything to this point renders as expected to the page. Say for example I
    render a TextBox child control from my collection. The user enters a value,
    then does a post back. How can I access the value of the TextBox control.
    Normally I would do this.Controls[childControlId] or a this.FindControl() but
    neither of these actions retrieve the control. In fact no child controls
    exist in the web control, I have implemented INamingContainer and my web
    control is dereived from Control.

    Any ideas on how I can get reference to my child controls after postback?

    Craig
    Craig Guest

  2. Similar Questions and Discussions

    1. Maintaining state in programmatically created composite user controls
      Hi I'm writing a timesheet application in ASP.NET (framework 1.1, VS 2003) that includes a user control to manage project time allocation...
    2. Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created)
      Ok here's my scenario. I have a Custom Composite Server Control (CCSC) consisting of a TextBox, Button & Panel. (And some other code - which I...
    3. Eventhandling from dynamically created controls
      I have some questions reguarding event handling of dynamically added controls. An example scenario (se code below): I have one button declared...
    4. Retrieving Values from dynamically created controls in a user control
      Hi, Dynamic control should be load every time page load, even on post back. take out control creation from IsPostBack condition. Natty Gur ...
    5. q: 'composite' control: works fine, but no children @ design time ?
      Hi All ! I have my control, which populates inner ControlCollection via custom control builder and AddParsedSubObject function. Everything works...
  3. #2

    Default Re: Composite Web Control and Dynamically Created Children Controls.

    Craig wrote:
    > Hi
    >
    > I am developing a web control which creates other web controls.
    > During the OnInit of the my web control I read an Xml file which
    > tells my web control which child control to create, adding the child
    > controls into a private collection.
    >
    > In the CreateChildControls (override) method, I loop through my
    > collection of child controls, adding them to the web control:
    > this.Controls.Add(this.Page.ParseControl(childCont rolCol[i]));
    >
    > I do not expose any properties of child controls in the web control.
    >
    > Everything to this point renders as expected to the page. Say for
    > example I render a TextBox child control from my collection. The
    > user enters a value, then does a post back. How can I access the
    > value of the TextBox control. Normally I would do
    > this.Controls[childControlId] or a this.FindControl() but neither of
    > these actions retrieve the control. In fact no child controls exist
    > in the web control, I have implemented INamingContainer and my web
    > control is dereived from Control.
    >
    > Any ideas on how I can get reference to my child controls after
    > postback?
    >
    > Craig
    Since your subcontrols are created dynamically, you should
    recreate them after postback.

    The best way to do this is to save enough information
    about the state of your control in ViewState, enough
    to reconstruct the control after postback (same number
    and order of subcontrols).

    If you manage to do that, accessing the postback values
    will work.

    These articles may inspire you:
    [url]http://www.dotnetjunkies.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/ctrlauth/templates/Repeater1.src&file=CS\Repeater1.cs[/url]
    [url]http://msdn.microsoft.com/library/en-us/dnaspp/html/databoundtemplatedcontrols.asp[/url]

    --

    Riki


    Riki Guest

  4. #3

    Default Re: Composite Web Control and Dynamically Created Children Controls.

    If you dynamically create controls then you are expected to dynamically
    create them again after postback.
    If Page.IsPostback though, don't set the value for the control. Let ASP.NET
    fill that in for you based on the user's selection.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    [url]http://SteveOrr.net[/url]


    "Craig" <Craig@discussions.microsoft.com> wrote in message
    news:F4FEDF86-897B-45A6-8897-AD8A24963D9D@microsoft.com...
    > Hi
    >
    > I am developing a web control which creates other web controls. During
    > the
    > OnInit of the my web control I read an Xml file which tells my web control
    > which child control to create, adding the child controls into a private
    > collection.
    >
    > In the CreateChildControls (override) method, I loop through my
    > collection
    > of child controls, adding them to the web control:
    > this.Controls.Add(this.Page.ParseControl(childCont rolCol[i]));
    >
    > I do not expose any properties of child controls in the web control.
    >
    > Everything to this point renders as expected to the page. Say for example
    > I
    > render a TextBox child control from my collection. The user enters a
    > value,
    > then does a post back. How can I access the value of the TextBox control.
    > Normally I would do this.Controls[childControlId] or a this.FindControl()
    > but
    > neither of these actions retrieve the control. In fact no child controls
    > exist in the web control, I have implemented INamingContainer and my web
    > control is dereived from Control.
    >
    > Any ideas on how I can get reference to my child controls after postback?
    >
    > Craig

    Steve C. Orr [MVP, MCSD] Guest

  5. #4

    Default Re: Composite Web Control and Dynamically Created Children Control

    Hi Riki

    Thanks for the reply. What is the reason for having a
    ViewState["ControlCount"]? I'm not sure if I understand how this will help.
    I was not able to reference my postback controls (created dynamically) by
    their ID, even though I set this in the Page.ParseControl(...) method. The
    only way I was able to retrieve the postback value was though
    Request.Forms[...] reference.

    Craig

    "Riki" wrote:
    > Craig wrote:
    > > Hi
    > >
    > > I am developing a web control which creates other web controls.
    > > During the OnInit of the my web control I read an Xml file which
    > > tells my web control which child control to create, adding the child
    > > controls into a private collection.
    > >
    > > In the CreateChildControls (override) method, I loop through my
    > > collection of child controls, adding them to the web control:
    > > this.Controls.Add(this.Page.ParseControl(childCont rolCol[i]));
    > >
    > > I do not expose any properties of child controls in the web control.
    > >
    > > Everything to this point renders as expected to the page. Say for
    > > example I render a TextBox child control from my collection. The
    > > user enters a value, then does a post back. How can I access the
    > > value of the TextBox control. Normally I would do
    > > this.Controls[childControlId] or a this.FindControl() but neither of
    > > these actions retrieve the control. In fact no child controls exist
    > > in the web control, I have implemented INamingContainer and my web
    > > control is dereived from Control.
    > >
    > > Any ideas on how I can get reference to my child controls after
    > > postback?
    > >
    > > Craig
    >
    > Since your subcontrols are created dynamically, you should
    > recreate them after postback.
    >
    > The best way to do this is to save enough information
    > about the state of your control in ViewState, enough
    > to reconstruct the control after postback (same number
    > and order of subcontrols).
    >
    > If you manage to do that, accessing the postback values
    > will work.
    >
    > These articles may inspire you:
    > [url]http://www.dotnetjunkies.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/ctrlauth/templates/Repeater1.src&file=CS\Repeater1.cs[/url]
    > [url]http://msdn.microsoft.com/library/en-us/dnaspp/html/databoundtemplatedcontrols.asp[/url]
    >
    > --
    >
    > Riki
    >
    >
    >
    Craig Guest

  6. #5

    Default Re: Composite Web Control and Dynamically Created Children Control

    On Sun, 13 Feb 2005 17:47:02 -0800, Craig wrote:
    > Hi Riki
    >
    > Thanks for the reply. What is the reason for having a
    > ViewState["ControlCount"]? I'm not sure if I understand how this will help.
    > I was not able to reference my postback controls (created dynamically) by
    > their ID, even though I set this in the Page.ParseControl(...) method. The
    > only way I was able to retrieve the postback value was though
    > Request.Forms[...] reference.
    >
    > Craig
    >
    > "Riki" wrote:
    >
    >> Craig wrote:
    >> > Hi
    >> >
    >> > I am developing a web control which creates other web controls.
    >> > During the OnInit of the my web control I read an Xml file which
    >> > tells my web control which child control to create, adding the child
    >> > controls into a private collection.
    >> >
    >> > In the CreateChildControls (override) method, I loop through my
    >> > collection of child controls, adding them to the web control:
    >> > this.Controls.Add(this.Page.ParseControl(childCont rolCol[i]));
    >> >
    >> > I do not expose any properties of child controls in the web control.
    >> >
    >> > Everything to this point renders as expected to the page. Say for
    >> > example I render a TextBox child control from my collection. The
    >> > user enters a value, then does a post back. How can I access the
    >> > value of the TextBox control. Normally I would do
    >> > this.Controls[childControlId] or a this.FindControl() but neither of
    >> > these actions retrieve the control. In fact no child controls exist
    >> > in the web control, I have implemented INamingContainer and my web
    >> > control is dereived from Control.
    >> >
    >> > Any ideas on how I can get reference to my child controls after
    >> > postback?
    >> >
    >> > Craig
    >>
    >> Since your subcontrols are created dynamically, you should
    >> recreate them after postback.
    >>
    >> The best way to do this is to save enough information
    >> about the state of your control in ViewState, enough
    >> to reconstruct the control after postback (same number
    >> and order of subcontrols).
    >>
    >> If you manage to do that, accessing the postback values
    >> will work.
    >>
    >> These articles may inspire you:
    >> [url]http://www.dotnetjunkies.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/ctrlauth/templates/Repeater1.src&file=CS\Repeater1.cs[/url]
    >> [url]http://msdn.microsoft.com/library/en-us/dnaspp/html/databoundtemplatedcontrols.asp[/url]
    >>
    >> --
    >>
    >> Riki
    >>
    >>
    >>
    From my limited understanding what needs to happen before you can
    reference your added controls, is that the controls need to be added to
    the DOM. Then the ViewState must be restored for those controls.
    Only then can you get at the controls and their data.

    intrader Guest

  7. #6

    Default Re: Composite Web Control and Dynamically Created Children Control

    Hi Steve

    This is precisely my issue, how do I re-create the controls on postback
    allowing asp.net to assign their values? I haven't implemented any of the
    IPostBackHandler... or IPostbackData... interfaces in my web control. Given
    my recent posts, can you offer or point me to a sample.

    Many thanks, Craig

    "Steve C. Orr [MVP, MCSD]" wrote:
    > If you dynamically create controls then you are expected to dynamically
    > create them again after postback.
    > If Page.IsPostback though, don't set the value for the control. Let ASP.NET
    > fill that in for you based on the user's selection.
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > [url]http://SteveOrr.net[/url]
    >
    >
    > "Craig" <Craig@discussions.microsoft.com> wrote in message
    > news:F4FEDF86-897B-45A6-8897-AD8A24963D9D@microsoft.com...
    > > Hi
    > >
    > > I am developing a web control which creates other web controls. During
    > > the
    > > OnInit of the my web control I read an Xml file which tells my web control
    > > which child control to create, adding the child controls into a private
    > > collection.
    > >
    > > In the CreateChildControls (override) method, I loop through my
    > > collection
    > > of child controls, adding them to the web control:
    > > this.Controls.Add(this.Page.ParseControl(childCont rolCol[i]));
    > >
    > > I do not expose any properties of child controls in the web control.
    > >
    > > Everything to this point renders as expected to the page. Say for example
    > > I
    > > render a TextBox child control from my collection. The user enters a
    > > value,
    > > then does a post back. How can I access the value of the TextBox control.
    > > Normally I would do this.Controls[childControlId] or a this.FindControl()
    > > but
    > > neither of these actions retrieve the control. In fact no child controls
    > > exist in the web control, I have implemented INamingContainer and my web
    > > control is dereived from Control.
    > >
    > > Any ideas on how I can get reference to my child controls after postback?
    > >
    > > Craig
    >
    >
    >
    Craig Guest

  8. #7

    Default Re: Composite Web Control and Dynamically Created Children Control

    Steve

    The xml structure goes something like this:

    <Report name='my report', description='my report description'>
    <Parm name='parm1'>
    <Control type='asp:TextBox' id='txtParm1' runat='server' text='report
    text'/>
    </Parm>
    </Report>

    The xml structure above represents a typical report file, there maybe
    multiple <Parm> element, but only 1 <Control ... /> element for each <Parm>.
    When the <Control> element is read, it uses ParseControl, assigning the
    Control to Parm.Control property which is has a type of Control.

    In the override CreateChildControls method I do this:

    foreach(Parm parm in report.Parms) {
    Controls.Add(parm.Control);
    }

    I'm not sure what to do on Postback, so I'm not sure what I am missing.

    "Steve C. Orr [MVP, MCSD]" wrote:
    > If you dynamically create controls then you are expected to dynamically
    > create them again after postback.
    > If Page.IsPostback though, don't set the value for the control. Let ASP.NET
    > fill that in for you based on the user's selection.
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > [url]http://SteveOrr.net[/url]
    >
    >
    > "Craig" <Craig@discussions.microsoft.com> wrote in message
    > news:F4FEDF86-897B-45A6-8897-AD8A24963D9D@microsoft.com...
    > > Hi
    > >
    > > I am developing a web control which creates other web controls. During
    > > the
    > > OnInit of the my web control I read an Xml file which tells my web control
    > > which child control to create, adding the child controls into a private
    > > collection.
    > >
    > > In the CreateChildControls (override) method, I loop through my
    > > collection
    > > of child controls, adding them to the web control:
    > > this.Controls.Add(this.Page.ParseControl(childCont rolCol[i]));
    > >
    > > I do not expose any properties of child controls in the web control.
    > >
    > > Everything to this point renders as expected to the page. Say for example
    > > I
    > > render a TextBox child control from my collection. The user enters a
    > > value,
    > > then does a post back. How can I access the value of the TextBox control.
    > > Normally I would do this.Controls[childControlId] or a this.FindControl()
    > > but
    > > neither of these actions retrieve the control. In fact no child controls
    > > exist in the web control, I have implemented INamingContainer and my web
    > > control is dereived from Control.
    > >
    > > Any ideas on how I can get reference to my child controls after postback?
    > >
    > > Craig
    >
    >
    >
    Craig Guest

  9. #8

    Default Re: Composite Web Control and Dynamically Created Children Control

    Whatever code you're running to create the controls in the first place, run
    it again upon each postback. Call it from the Page_Init or Page_Load event.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    [url]http://SteveOrr.net[/url]


    "Craig" <Craig@discussions.microsoft.com> wrote in message
    news:22612A83-0A74-4375-B4F8-371EABAB0FA7@microsoft.com...
    > Steve
    >
    > The xml structure goes something like this:
    >
    > <Report name='my report', description='my report description'>
    > <Parm name='parm1'>
    > <Control type='asp:TextBox' id='txtParm1' runat='server' text='report
    > text'/>
    > </Parm>
    > </Report>
    >
    > The xml structure above represents a typical report file, there maybe
    > multiple <Parm> element, but only 1 <Control ... /> element for each
    > <Parm>.
    > When the <Control> element is read, it uses ParseControl, assigning the
    > Control to Parm.Control property which is has a type of Control.
    >
    > In the override CreateChildControls method I do this:
    >
    > foreach(Parm parm in report.Parms) {
    > Controls.Add(parm.Control);
    > }
    >
    > I'm not sure what to do on Postback, so I'm not sure what I am missing.
    >
    > "Steve C. Orr [MVP, MCSD]" wrote:
    >
    >> If you dynamically create controls then you are expected to dynamically
    >> create them again after postback.
    >> If Page.IsPostback though, don't set the value for the control. Let
    >> ASP.NET
    >> fill that in for you based on the user's selection.
    >>
    >> --
    >> I hope this helps,
    >> Steve C. Orr, MCSD, MVP
    >> [url]http://SteveOrr.net[/url]
    >>
    >>
    >> "Craig" <Craig@discussions.microsoft.com> wrote in message
    >> news:F4FEDF86-897B-45A6-8897-AD8A24963D9D@microsoft.com...
    >> > Hi
    >> >
    >> > I am developing a web control which creates other web controls. During
    >> > the
    >> > OnInit of the my web control I read an Xml file which tells my web
    >> > control
    >> > which child control to create, adding the child controls into a private
    >> > collection.
    >> >
    >> > In the CreateChildControls (override) method, I loop through my
    >> > collection
    >> > of child controls, adding them to the web control:
    >> > this.Controls.Add(this.Page.ParseControl(childCont rolCol[i]));
    >> >
    >> > I do not expose any properties of child controls in the web control.
    >> >
    >> > Everything to this point renders as expected to the page. Say for
    >> > example
    >> > I
    >> > render a TextBox child control from my collection. The user enters a
    >> > value,
    >> > then does a post back. How can I access the value of the TextBox
    >> > control.
    >> > Normally I would do this.Controls[childControlId] or a
    >> > this.FindControl()
    >> > but
    >> > neither of these actions retrieve the control. In fact no child
    >> > controls
    >> > exist in the web control, I have implemented INamingContainer and my
    >> > web
    >> > control is dereived from Control.
    >> >
    >> > Any ideas on how I can get reference to my child controls after
    >> > postback?
    >> >
    >> > Craig
    >>
    >>
    >>

    Steve C. Orr [MVP, MCSD] Guest

  10. #9

    Default Re: Composite Web Control and Dynamically Created Children Controls.

    On Sun, 13 Feb 2005 10:27:01 -0800, "Craig"
    <Craig@discussions.microsoft.com> wrote:
    >Hi
    >
    >I am developing a web control which creates other web controls. During the
    >OnInit of the my web control I read an Xml file which tells my web control
    >which child control to create, adding the child controls into a private
    >collection.
    >
    >In the CreateChildControls (override) method, I loop through my collection
    >of child controls, adding them to the web control:
    > this.Controls.Add(this.Page.ParseControl(childCont rolCol[i]));
    >
    >I do not expose any properties of child controls in the web control.
    >
    >Everything to this point renders as expected to the page. Say for example I
    >render a TextBox child control from my collection. The user enters a value,
    >then does a post back. How can I access the value of the TextBox control.
    >Normally I would do this.Controls[childControlId] or a this.FindControl() but
    >neither of these actions retrieve the control. In fact no child controls
    >exist in the web control, I have implemented INamingContainer and my web
    >control is dereived from Control.
    >
    >Any ideas on how I can get reference to my child controls after postback?
    >
    >Craig
    After postback, CreateChildControls is executed twice. The first time
    you need to build the exact web page before the postback, using some
    ViewState properties that indicates the previous state of your web
    form.

    Then you could find your control on the page with FindControl and get
    its value, and sets the ViewState properties to the new state.

    The second time, you build the new page according to the new
    viewstate.

    Be sure to implement IPostBackDataHandler, IPostBackEventHandler,
    INamingContainer.


    --
    Ludwig Stuyck
    [url]http://www.coders-lab.net[/url]
    Ludwig Stuyck Guest

  11. #10

    Default Re: Composite Web Control and Dynamically Created Children Controls.

    Also, be sure to re-create the child controls in the first PostBack
    CreateChildControls in the same order and with the same structure as it
    was when the control last rendered, so that the ViewState goes into the
    correct control. According to "ASP.NET Server Controls and Components"
    (p. 311) the base Control class rehydrates viewstate into the controls
    using an index, not the ID or type of the child controls.

    ->AlanM

    AlanM 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