Dynamic Control Loading Problem

K

Kirsta Lemieux

Hello,
I have been working on a dynamically generated control,
that after traveling through a maze of problems I finally
reached the end, only to find a really serious one that is
keeping me awake at night. This is a pretty complex
problem, which I have been trying to solve for days,
looking at it from different angles, and no luck, so this
is my last resort before giving up. Here is the
description what I have done:

I have built a UserControl (an ascx file), that has
multiple dropdown boxes, and based on the value change of
one of them, the other dropdown boxes (their list of
values) reload based on the current selected information.
The way that I do that is detect whether the SelectedIndex
of the Dropdown box changed (in the PageLoad section). The
reason why I'm doing this in the PageLoad is because from
the selected information, I dynamically add other controls
to the webform (or my control), so that's why I need all
that before my page is rendered. This all works great,
just the way I want it. So the way I tested it, is created
a WebForm, and then dragged and dropped my control. And
when I go to my created page it works great...

But now here comes the tricky part. In my other page,
based on some specific event, I want to DYNAMICALLY LOAD
my control. So I used the Page.LoadControl
("MyControl.ascx") when I detect the event (also in
PageLoad). Ok, so I tested it, and the control loads fine
on the event. But then when I change the selection in one
of my dropdown boxes, at the PageLoad I can no longer
retrieve the SelectedIndex (it is still unselected at that
time). Basically as if the the Form State is not loaded in
at that time (but when I dragged and dropped it in, it did
load before reaching my code in the PageLoad). Any Idea
why is that happening?

If what I wrote above is not clear enough, I included some
sample steps that you can try it out to reproduce the
problem:

PART A
Create a new ASCX file and call it TestControl.ascx
In There:
Add a DropDownList to the Form, and call it lstTest.
Add a Label to the Form, and call it Label1.
Set the lstTest.AutoPostback to True
In the Code Section of the ASCX file:

Public Sub Page_Load(...standard stuff)
If lstTest.SelectedIndex > 0 Then
Label1.Text = "Proves that it Works".
End If

If not IsPostBack Then
lstTest.Items.Add("Test Value 1")
lstTest.Items.Add("Test Value 2")
lstTest.Items.Add("Test Value 3")
End If

End Sub


PART B
Create a new ASPX File and call it Test.aspx

Method 1 (Drag and Drop):
Simply Drag and drop the TestControl.ascx to the Test.aspx
webform. When you run it, and select "Test Value 3", you
will notice that that Label text changes to: "Proves that
it Works". And that's exactly what I want. But then:

Method 2 (Dynamic Loading)
Instead of dragging and dropping the TestControl.ascx, add
a PlaceHolder control to the webform and call it
PlaceHolder1. Once you have done that, add the following
in the Page_Load section of the Test.aspx code:

Dim aControl As UserControl = Page.LoadControl
("TestControl.ascx")
aControl.ID = "MyTestControl1"
PlaceHolder1.Controls.Add(aControl)

And then run the page. As you see it appears correctly,
and the dropdown box is loaded. But then when you
select "Test Value 3", when the page is Loaded by
AutoPostback the text of the Label does not change, but
the "Test Value 3" remains selected. If you debug it with
Visual Studio and break at the line: "If
lstTest.SelectedIndex > 0 Then" you will notice that the
selected index is still 0 (and it should be 2).

Any suggestions. This limits a lot of things when trying
to load controls dynamically.

Thank you for your help in advance. If you need the sample
project code, e-mail me at (e-mail address removed) and
I'll send it to you.

Merci,
Kirsta Lemieux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top