Dynamically create non-fixed number of TextBoxes in Composite Control

I have a composite control(TextBoxControl.cs) in my
DynamicTextBox.aspx. On the Page_Load of the
DynamicTextBox.aspx page, I am calling a business tier
assembly, which is returning an ArrayList containing an
unknown amount of objects. I would like to pass this
ArrayList to my composite control and for each object in
the ArrayList, I would like to build and populate a
TextBox. The reason I need to pass the data to the
composite control is because I would like to reuse this
control on multiple pages in the application.

Currently, I am able to get the data from the business
tier assembly directly from the composite control's
CreateChildControls() method, and create the Textboxes.
The problem I am having is passing the data from the
Page_Load event of the parent DynamicTextBox.aspx and
having the controls show up on the page. The data gets to
the control's CreateChildControls() event and I watch it
in Debug go through and create the controls, but when the
page renders, the controls are not there. What gives? I
tried passing the data from the DynamicTextBox.aspx
page's On_Init, but the same result as Page_Load occured.

Does anyone know why the TextBoxes are not rendering or
have any insight as to what I am doing wrong?

Advertisements

"Paul" <> wrote in message
news:047201c35d36$fc66ab70$...
> Hi,
>
> Does anyone know why the TextBoxes are not rendering or
> have any insight as to what I am doing wrong?

Paul, I suggest you read up on creating DataBound controls. "Developing
Microsoft® ASP.NET Server Controls and Components" from MS Press, by Nikhil
Kothari and Vandana Datye (http://www.microsoft.com/mspress/books/5728.asp)
is the best book I've seen on the subject.
> Thanks in advance for your time and help,

I've never created a data bound control, but from what I recall reading, you
don't want to create the controls in CreateChildControls, but rather in
DataBind. Also, you'll want to be able to recreate the controls from
ViewState in CreateChildControls. The book above recommends having a common
control-creation method called both by DataBind and by CreateChildControls -
they call theirs CreateControlHierarchy, and it takes a bool to tell it
whether to use the data source or to use ViewState.
--
John Saunders
Internet Engineer

Advertisements

I am still unclear how to pass data to the control before
the textboxes are rendered. Unfortunately our deadline
is such that I can't take the gamble of thinking I can
figure it out using the book. If I can't find an answer,
I will have to move to code directly into the page as
opposed to using a control. Even then I am unsure if
that will work.

"Paul" <> wrote in message
news:045401c362b4$adbe48b0$...
> I am unable to do this because the properties don't exist
> at the time CreateChildControls() runs. I set the
> properties during the Page_Load of the parent.
>
> Is this an example of a time when I would need to use a
> superclassed control? I have never wrote one, but I am
> reading on them now. Still unclear how to resolve this
> issue.

Why is CreateChildControls running before Page_Load? That's almost certainly
wrong.

I had a control doing something like that due to EnsureChildControls calls
at the wrong times. Removing them fixed the control.
--
John Saunders
Internet Engineer

Sorry for the confusion, I was referring to Page_Load of
the parent page which contains the control. If you
follow through a debug, you can see the path.

I found a fix!!! I was reading a book from Jeff Prosise
called "Programming Microsoft .NET" and saw the light.
On page 338 he explains how to dynamically load controls
onto the page. I set asplaceholders on the page where
I want my control to be rendered, then did this in my
code behind of the parent page:

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!