Ask a Question related to ASP.NET Building Controls, Design and Development.
-
Craig #1
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
-
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... -
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... -
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... -
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 ... -
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... -
Riki #2
Re: Composite Web Control and Dynamically Created Children Controls.
Craig wrote:
Since your subcontrols are created dynamically, you should> 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
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
-
Steve C. Orr [MVP, MCSD] #3
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
-
Craig #4
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
-
intrader #5
Re: Composite Web Control and Dynamically Created Children Control
On Sun, 13 Feb 2005 17:47:02 -0800, Craig wrote:
From my limited understanding what needs to happen before you can> 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
>>
>>
>>
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
-
Craig #6
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
-
Craig #7
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
-
Steve C. Orr [MVP, MCSD] #8
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
-
Ludwig Stuyck #9
Re: Composite Web Control and Dynamically Created Children Controls.
On Sun, 13 Feb 2005 10:27:01 -0800, "Craig"
<Craig@discussions.microsoft.com> wrote:
After postback, CreateChildControls is executed twice. The first time>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
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
-
AlanM #10
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



Reply With Quote

