Help with User Control, please.

G

Guest

Hello All,

I am developing a webform which creates ArrayLists of people's names and
addresses (the values are retrieved from an xml file) and dynamically drops a
user control onto the webform and populates it with the required names. Once
the user selects a name, the webform will then drop another user control (the
same control) onto itself and populate it with the selected name's addresses.

The user control is a combination of an asp:label and an asp:dropdownlist.
(The asp:label just indicates what the contents of the dropdownlist are -
name, address, whatever.)

The problem is this: I can drop the user control with the names onto the
page without any difficulty. The list of names appears for the user to
select from.

The AddHandler doesn't seem to connect the dropdownlist event with the
selection being changed. I've even tried to comment out the Addhandler code
and use the actual handler created by VS Studio instead:

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
m_SelectedId = Integer.Parse(CType(sender,
DropDownList).SelectedItem.Value)
End Sub

Again, no luck.

I need to retrieve the selected and use it to retrieve the person's addresses.

I would apprciate any help with this that you could offer. This one is
stumping me.

Here is the code in the webform which instantiates and adds the the user
control to a tablecell in a table on the webform:

Dim UControl As AugmentedDropDownList =
CType(LoadControl("../UserControls/AugmentedDropDownList.ascx"),
AugmentedDropDownList)

UControl.PopulateDropDownList(Control.Attributes("name").Value, DropDownNode)

NewCell.Controls.Add(UControl)

Here is the code in the entire User Control:

Imports System.Xml

Public Class AugmentedDropDownList
Inherits System.Web.UI.UserControl

Private m_SelectedId As Integer

Public Sub PopulateDropDownList(ByVal ListCaption As String, ByVal
ListItems As XmlNode)
Label1.Text = ListCaption
Dim Items As XmlNodeList = ListItems.SelectNodes("Items/Item")
For Each Item As XmlNode In Items
Dim li As New ListItem
li.Value = Item.Attributes("id").Value
li.Text = Item.Attributes("value").Value
DropDownList1.Items.Add(li)
Next
m_SelectedId = 0
AddHandler DropDownList1.SelectedIndexChanged, AddressOf
DropDownHandler
End Sub

Private Sub DropDownHandler(ByVal sender As System.Object, ByVal e As
System.EventArgs)
m_SelectedId = Integer.Parse(CType(sender,
DropDownList).SelectedItem.Value)
End Sub
End Class

TIA,
 
G

Guest

Hi Joe,

1) Dynamically loaded controls should be loaded during the Page
Initialization phase to maintain their ViewState upon postback:

http://msdn.microsoft.com/library/d...guide/html/cpconcontrolexecutionlifecycle.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp


2) In the case where you have to load the control late within the page
lifecycle (e.g., while responding to another server control event ) then
save in the Session a variable that allows you to reload it within the Init
event handling upon postback, e.g.

//put a line like this when you create the datagrid
Session("NameOfControlThattWasLoaded") = "Control1";

//in the init event handling of control2 put code similar to this
if (Page.IsPostBack)
{
if (Session("NameOfControlThattWasLoaded"))
{
//reload the control
}
}


I have a demo to demonstrate the concept on this link:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top