473,612 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Not getting values from DropDownLists in User Controls

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.Ena bleViewState 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.Se lectedIndex 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
Feb 21 '06 #1
8 1562
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/

Feb 21 '06 #2
Have you tried;

Dim strValue as String = dropdown1.Selec tedValue

Does that still come up emtpy? What about Request.Form("d ropdown1")?
Jeremy Reid
http://hgtit.com

Feb 22 '06 #3
Darren Kopp wrote:
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.Reflecti on.PropertyInfo
Dim UCType As Type

For Each r In ControlTable.Ro ws
UC = FindControl("Pa nel1").FindCont rol(r("Name"))
If Not IsNothing(UC) Then
Dim strVal As String
If Not r("DBField").Ge tType Is Type.GetType("S ystem.DBNull") Then
UCType = UC.GetType
UCprop = UCType.GetPrope rty("dbVal")
If Not IsNothing(UCpro p) 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
Feb 22 '06 #4
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.U serControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

End Sub
Public WithEvents DropDownList1 As
System.Web.UI.W ebControls.Drop DownList

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

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

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

End Sub
Protected WithEvents postbackbutton As
System.Web.UI.W ebControls.Butt on
Protected WithEvents result As System.Web.UI.W ebControls.Labe l

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

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

#End Region
Protected WithEvents DDLUC As ddl

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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.EventArg s) Handles postbackbutton. Click
DDLUC = Page.FindContro l("MyUC")

result.Text = DDLUC.DropDownL ist1.SelectedVa lue

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/

Feb 22 '06 #5
Darren Kopp wrote:
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.U serControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

End Sub
Public WithEvents DropDownList1 As
System.Web.UI.W ebControls.Drop DownList

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

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

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

End Sub
Protected WithEvents postbackbutton As
System.Web.UI.W ebControls.Butt on
Protected WithEvents result As System.Web.UI.W ebControls.Labe l

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

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

#End Region
Protected WithEvents DDLUC As ddl

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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.EventArg s) Handles postbackbutton. Click
DDLUC = Page.FindContro l("MyUC")

result.Text = DDLUC.DropDownL ist1.SelectedVa lue

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
Feb 22 '06 #6
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

Feb 22 '06 #7
Darren Kopp wrote:
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
Feb 22 '06 #8
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/

Feb 22 '06 #9

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

Similar topics

2
1680
by: Billy Jacobs | last post by:
Fairly Complicated Problem: 1. I have a C# web application. 2. I have a web form with 3 drop down list controls and other text box controls for updating user information. 3. There is a submit button. 4. On the page_load of the webform I databind three dropdownlist controls to a different datareaders. Results: 1. When I run the page displays correctly with the
2
2753
by: Alan Lambert | last post by:
I have a web page that contains, amongst other things, a DropDownList called drpProjectCodes) and a Panel (called pnlStatus) which contains six Checkboxes. drpProjectCodes contains a list of project codes plus an 'All' option. If All is selected then the user can selected statuses using the CheckBoxes otherwise the CheckBoxes are disabled by disabling pnlStatus. Several of the Checkboxes are checked by default. There are actually other...
9
2334
by: james.e.coleman | last post by:
Hello, I have created a custom dropdownlist that is used multiple times within a single page. When trying to set the values of the controls with the page in which they are being used, they all are set to the value of the last control. I found this link: http://www.codecomments.com/archive315-2005-1-366113.html which I think explains the situation but I'm not sure how to implement the solution in my case since I am not using a .ascx...
7
5476
by: charliewest | last post by:
Hello - I'm using a Repeater control to render information in a very customized grid-like table. The Repeater control is binded to a DataSet with several records of information. Within the Repeater control, I've placed DropDownLists and CheckBoxes. When the user has updated the information, he/he clicks the submit button which is outside the scope of the Repeater control.
0
2384
by: reddog | last post by:
Getting the SelectedIndex of a DropDownlist under the ItemTemplate in a GridView is harder than I thought it would be. I have: A Gridview of n rows bound to a DataSet. A DropDownList as an ItemTemplate in the GridView populated with three ListItems. A Button to Submit the form.
0
1670
by: tofu.captain | last post by:
I have an ASPX page that needs to read in form values (txtbox, dropdownlists, etc) from a custom control that redirects execution by crosspage postback. Here is the how it is being redirected: <asp:Button ID="btnViewReport" runat="server" Text="View Report" PostBackUrl="~/Viewer.aspx" /> The custom control is sitting in a Web Part Zone Declarative Catalog: <asp:CatalogZone ID="CatalogZone1" runat="server" >
1
1669
by: Roffers | last post by:
Okay I have a parent page, we'll call it parent.aspx and it holds the following two user controls <uc1:Box1 id="Box1" runat="server"></uc1:Box1<----THIS CONTAINS A DROP DOWN LIST <br> <uc1:Box2 id="Box2" runat="server"></uc1:Box2<----THIS CONTAINS A DATALIST In Box1, I have a dropdown list that gets selected and should set
0
1108
by: Nightcrawler | last post by:
I have a table adapter that looks like this: TableAdapter.GetData(string name, string email, ing countryid, int stateId) This is calling the following stored procedure SELECT * FROM Table WHERE Name= COALESCE(@Name,Name)
4
8547
by: pankajsingh5k | last post by:
Hi guys, These question is for all the experts... Please help me before my brain explodes The problem is again with the formview control.. I have a formview and i have to use it that when it displays it shows values from the database and i have a link button edit on the ItemTemplate that changes the mode to edit mode and allows to edit the values which can be edited...
0
8173
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8115
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8617
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8254
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6082
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5537
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2555
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1416
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.