Not getting values from DropDownLists in User Controls

M

MattB

I have a asp.net 1.1/vb application that has a page with a bunch of
dynamically added User Controls.
When I add the controls, I set the UserControl.EnableViewState to true.
For all my controls with TextBoxes, I can get the value back out of the
controls just fine.
One of my User Controls has some DropDownLists as well as a TextBox.
I get a value from the TextBox on postback, but not the DropDownLists.
Even if I see a value selected when the page displays, my
DropDownList.SelectedIndex is -1 when I submit the form and try to get
all the values back out. I double-checked the DropDownList controls
themselves, and they have EnableViewState = True too.
Any suggestions as to how to get the values back out from the
DropDownLists? Surely I'm overlooking something!
TIA!

Matt
 
B

blackstaronline.net

Have you tried;

Dim strValue as String = dropdown1.SelectedValue

Does that still come up emtpy? What about Request.Form("dropdown1")?


Jeremy Reid
http://hgtit.com
 
M

MattB

Darren said:
Are you trying to access the drop down list from the user control or
the page hosting the user control?

Darren Kopp
http://blog.secudocs.com/

Thanks for the reply.

I have public properties in my User Control for data access.
So I loop through the rows of the DataTable I used to put the controls
on the page to get the values out when the form is submitted like this:

Dim r As DataRow
Dim UC As UserControl
Dim UCprop As System.Reflection.PropertyInfo
Dim UCType As Type

For Each r In ControlTable.Rows
UC = FindControl("Panel1").FindControl(r("Name"))
If Not IsNothing(UC) Then
Dim strVal As String
If Not r("DBField").GetType Is Type.GetType("System.DBNull") Then
UCType = UC.GetType
UCprop = UCType.GetProperty("dbVal")
If Not IsNothing(UCprop) Then
strVal = UCprop.GetValue(UC,Nothing)
End If
....

The weird thing is, a TextBox control in the very same User Control DOES
return the correct value, just not the DropDownLists.

Matt
 
D

Darren Kopp

wow... you are gonna love me because i am going to make your life so
much easier. Please forgive me if I screw up my vb.net syntax though.

I'll paste the relevant code first...

[User Control]
Public Class ddl
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Public WithEvents DropDownList1 As
System.Web.UI.WebControls.DropDownList

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

End Class

[WebForm]
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents postbackbutton As
System.Web.UI.WebControls.Button
Protected WithEvents result As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Protected WithEvents DDLUC As ddl

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub postbackbutton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles postbackbutton.Click
DDLUC = Page.FindControl("MyUC")

result.Text = DDLUC.DropDownList1.SelectedValue

End Sub

Ok, so basically what you need to look at is that in the UC i have
DropDownList1 (the the only one on the page) as public, rather than
protected which is default. On the webform, i have declared Protected
WithEvents DDLUC as ddl. ddl is the uc's class (public class ddl).
Now, on my webform i have 3 items- the user contol, the button, and the
label. on my Click event, i set DDLUC (my protected class variable)
equal to the value of the user control (MyUC is the ID of the control
in the webform). Then you just access the DropDownList1 to get the
selected value.

You can take this further by keeping the dropdownlist1 as protected and
having properties that return what you want. However you do it, you
can do it without all that reflection mumbo jumbo.

HTH,
Darren Kopp
http://blog.secudocs.com/
 
M

MattB

Darren said:
wow... you are gonna love me because i am going to make your life so
much easier. Please forgive me if I screw up my vb.net syntax though.

I'll paste the relevant code first...

[User Control]
Public Class ddl
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Public WithEvents DropDownList1 As
System.Web.UI.WebControls.DropDownList

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

End Class

[WebForm]
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents postbackbutton As
System.Web.UI.WebControls.Button
Protected WithEvents result As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Protected WithEvents DDLUC As ddl

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub postbackbutton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles postbackbutton.Click
DDLUC = Page.FindControl("MyUC")

result.Text = DDLUC.DropDownList1.SelectedValue

End Sub

Ok, so basically what you need to look at is that in the UC i have
DropDownList1 (the the only one on the page) as public, rather than
protected which is default. On the webform, i have declared Protected
WithEvents DDLUC as ddl. ddl is the uc's class (public class ddl).
Now, on my webform i have 3 items- the user contol, the button, and the
label. on my Click event, i set DDLUC (my protected class variable)
equal to the value of the user control (MyUC is the ID of the control
in the webform). Then you just access the DropDownList1 to get the
selected value.

You can take this further by keeping the dropdownlist1 as protected and
having properties that return what you want. However you do it, you
can do it without all that reflection mumbo jumbo.

HTH,
Darren Kopp
http://blog.secudocs.com/

Cool. Thanks, I'll check that out.
I'm not totally sure if this will work for me because this is part of a
whole framework I've built to allow for any number of user controls to
be configured via an XML file.
Since the different UC's may have different control types (labels,
DropDownLists, TextBoxes) I decided to use the Reflection "mumbo jumbo"
to match fields/columns in my XML file with properties in the user
controls. This should help facilitate adding different UCs, and if they
need to have additional properties I can add those to the XML config
file without changing the base code.
Do you know if I can do that kind of thing with simpler code?

Matt
 
D

Darren Kopp

I'm not sure I understand exactly what you are saying. Are you talking
about a single user control or many different user controls. And
within that user control, is there just one control or are there many
different types of controls.

-Darren
 
M

MattB

Darren said:
I'm not sure I understand exactly what you are saying. Are you talking
about a single user control or many different user controls. And
within that user control, is there just one control or are there many
different types of controls.

-Darren

There would typically be many user controls. This application gets
redistributed and this enables our clients to configure what user
controls appear on the page.

Each user control will have several different controls on it.

The one I was having trouble with was for entering a height in either
English or Metric units. The English units has two dropdowns, one for
Feet and one for Inches, but the database only has a value for total
inches. So when I add the control to the page and populate it, I just
hand the database value if total Inches to the control via a Public
Property (called dbVal) in the UC. Then in Set part of that property, I
computer the Ft/In values and populate the controls. So the UC-specific
logic is in the UC, and I have a standard interface to allow looping
through the controls to assign or retrieve values from the database.

Thanks for taking the time to look at this! Hope this makes a little
more sense.

Matt
 
D

Darren Kopp

Ok... so i take it you are looping through the variables in the
database and creating a new instance of your UC on the page, passing in
the value from the db to dbVal, which then populates the drop down
lists. If you could post or email me the relevant code, that would be
very helpful. my email is darrenkopp [at] [gmail.com]. Since this is
for a business application, I understand if that's not possible.

The areas I am interested most in is a) how you are adding controls to
the User Control and b) how you are adding the user controls to the
page. I would check how and where you are adding the controls.
Dynamically added controls should be added in the Page_PreInit, so I
would check that out. This link may help you out.
http://msdn2.microsoft.com/en-us/library/ms178472.aspx.

If you can post the code them i'm more than willing to take a look at
it, and offer suggestions / solutions. As you mentioned this is part
of a framework, so I know that things that I recommend may not be
feasible with how the system is designed, but maybe they will spark an
idea on your end :D.

Darren Kopp
http://blog.secudocs.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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top