Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Getting te ID of a Selected Item in a dropdownlist.

Reply
Thread Tools

Getting te ID of a Selected Item in a dropdownlist.

 
 
Rob Venable
Guest
Posts: n/a
 
      01-20-2004

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!
 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      01-20-2004
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>
<aspropDownList id="DropDownList1" runat="server"
AutoPostBack="True"></aspropDownList></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>


Ken
MVP [ASP.NET]

"Rob Venable" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> 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!


 
Reply With Quote
 
 
 
 
Rob Venable
Guest
Posts: n/a
 
      01-21-2004

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!
 
Reply With Quote
 
Marshal Antony
Guest
Posts: n/a
 
      01-21-2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> 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!



 
Reply With Quote
 
Rob Venable
Guest
Posts: n/a
 
      01-21-2004

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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help to fill textboxes from a selected item in datagrid-selected index changed. mldardy ASP .Net 0 09-28-2010 02:59 PM
selected=selected in the center of option list John HTML 5 03-01-2010 08:30 PM
Selected ListItem in DropDownList does not appear selected Nathan Sokalski ASP .Net 0 10-05-2008 10:29 PM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
[Struts] Nested beans and getting the selected item value in a drop down form jgalzic@gmail.com Java 2 10-02-2005 02:23 AM



Advertisments