473,396 Members | 1,812 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,396 software developers and data experts.

Getting te ID of a Selected Item in a dropdownlist.


Hi all,
Can anyone tell me how to retrieve the ID of a selected item from a
DropDownList. My query brings back the ID and value and I cycle through
the .Read Method to add items to the DropDownList.
e.g.
While dr.Read
DDL.Items.Add(dr("value"))
End While

I can do it if I use this method:
DDL.DataSource = dr
DDL.DataTextField = "value"
DDL.DataValueField = "id"
DDL.DataBind()

and then refer the the DataValueField to get the ID.

This would be the easy way but I have to do it by adding items to the
DDL.
Is there a property I can set to make it equal to the ID and then be
able to retrieve it?

Thanks

Rob
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
4 3093
Hey Rob,

Is it adding the list items to the ddl that is giving you trouble? If so,
here's a way to add them via the reader and display the ID when the ddl is
changed.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim myCMD As SqlCommand = _
New SqlCommand _
("SELECT CategoryID, CategoryName FROM Categories", _
SqlConnection1)
SqlConnection1.Open()
Dim dr As SqlDataReader = myCMD.ExecuteReader()
Dim lstItem As ListItem
While dr.Read
lstItem = New ListItem
lstItem.Text = dr.Item("CategoryName")
lstItem.Value = dr.Item("CategoryID")
DropDownList1.Items.Add(lstItem)
End While
dr.Close()
SqlConnection1.Close()
End If
End Sub
Private Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
Label1.Text = DropDownList1.SelectedItem.Value.ToString
End Sub

<P>
<asp:DropDownList id="DropDownList1" runat="server"
AutoPostBack="True"></asp:DropDownList></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
Ken
MVP [ASP.NET]

"Rob Venable" <rv******@rogers.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

Hi all,
Can anyone tell me how to retrieve the ID of a selected item from a
DropDownList. My query brings back the ID and value and I cycle through
the .Read Method to add items to the DropDownList.
e.g.
While dr.Read
DDL.Items.Add(dr("value"))
End While

I can do it if I use this method:
DDL.DataSource = dr
DDL.DataTextField = "value"
DDL.DataValueField = "id"
DDL.DataBind()

and then refer the the DataValueField to get the ID.

This would be the easy way but I have to do it by adding items to the
DDL.
Is there a property I can set to make it equal to the ID and then be
able to retrieve it?

Thanks

Rob
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 18 '05 #2

Thanks Ken,
That works fine but I still have one little problem. I need to add a
blank entry in my dropdownlist because this select box is not a
mandatory field on my form.
If I instantiate the listitem inside my while loop, how can I add a
blank line at the top?

Thanks for your help.

Rob

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
Rob,

This is the code you need.

DropDownlist.Items.Insert(0,new ListItem(" "," "));

This will add a blank item to the first position of the drop down list.

Ragards,

Marshal Antony

http://dotnetmarshal.com


"Rob Venable" <rv******@rogers.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...

Thanks Ken,
That works fine but I still have one little problem. I need to add a
blank entry in my dropdownlist because this select box is not a
mandatory field on my form.
If I instantiate the listitem inside my while loop, how can I add a
blank line at the top?

Thanks for your help.

Rob

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #4

Thanks Marshal,

That's exactly what I needed.

Regards,
Rob
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #5

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

Similar topics

1
by: Aaron Prohaska | last post by:
I'm having the problem with this drop down list on postback. For some reason both the ListItems get selected when I change the selected item. Using the code below I'm building the drop down list in...
5
by: Kris Rockwell | last post by:
Hello (again), I have gotten the dropdown list functionality to work through a few tricks (probably not the most efficient, but it works) but I am not sure how to set the default selected value....
2
by: huzz | last post by:
How do i make a dropdownlist selected value based on the value i retrive from the database. Basically i have an edit page and like to display the default value in a dropdown list from the...
4
by: wolfgang wagner | last post by:
hi all! after successfully integrating a dropdownlist in my datagrid i have another problem: i cannot set the selected index of the dropdownlistbox. here is my code: hardware.aspx...
6
by: Jenna Alten | last post by:
I have a datagrid with a template column that contains a dropdown list. I currently fill and display the dropdown list on the page load. This is working correctly. I am NOT using an Edit Column. I...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
4
by: Joe Schmoe | last post by:
All I want to to be able to take a two-column DataReader (One column with the Item ID number, the other with Item Description text) and load it into a Windows Forms ComboBox (Set to DropDownList...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
3
by: Iain | last post by:
Hi All I have 2 DropDownList boxes on a page. The first (id= "Operation") is populated on PageLoad with the contents of a database table. The second id="WorkStations" will not be populated...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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...
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.