473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Help with User Control, please.

Joe
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,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
Dec 12 '05 #1
3 1488
Joe
Thanks anyway. I got it. Needed to set AutoPostBack to true!
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Joe" wrote:
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,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation

Dec 12 '05 #2
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/de...nlifecycle.asp

http://msdn.microsoft.com/library/de.../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/Dy...dControls.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Joe" wrote:
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,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation

Dec 12 '05 #3
Joe
Phillip,

Thanks.
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Phillip Williams" wrote:
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/de...nlifecycle.asp

http://msdn.microsoft.com/library/de.../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/Dy...dControls.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Joe" wrote:
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,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation

Dec 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
13
by: Lloyd Sheen | last post by:
I have now spent 5 hours on google/msdn looking for something useful in the creation of user controls for asp.net. The VS 2003 has very limited support for things such as absolute positioning of...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
5
by: Dan Nash | last post by:
Hi all, I've got a page with a user control on, added via VS. I'm trying to get to a property of the user control (or more precisely, a public var). Here's the code at the top of my aspx...
3
by: Tim::.. | last post by:
Can someone please help... I'm kind of new to asp.net and VS.net and need to know how to get the value of a variable in the following funtion into a sub on another aspx page! Can someone please...
3
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
11
by: Brad Baker | last post by:
I'm building a small web application - I started out placing all my code in one file (config.aspx). As I continue to add code though it was becoming very unwieldy. After doing some searching...
3
by: Robert Dufour | last post by:
I am trying to localize a very simple web site (english and french) I have a master page and one content page for now. On the master page I have placed a Localize control On the content page I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.