Populating Dropdown Lists
-
We've recently moved over from ASP 3 for our front end to ASP.NET (using VB.NET sadly). In the past we'd populate our dropdown lists for searches so that the value and the display showed different fields from our database. For example, to allow the user to select a firm in a search, the value might hold the firm id, but the display would be the firm name. Now I'm trying to do the same thing with ASP.NET. Using
cmbFirms.Items.Add(.Item("firm_name"))
for example (where .Item("firm_name")) is in a with block where a recordset is being read out) results in both the value and the display obviously containing the firm's name. Is there a way to give the value of the option selected a different value from the display? I've been trying things like :cmbFirms.SelectedIndex.Equals(.Item("firm_id")) cmbFirms.SelectedValue.Equals(.Item("firm_name"))
andcmbFirms.Items.IndexOf(.Item("firm_id")) cmbFirms.DataValueField = .Item("firm_id")
in desperation to set the index to the firm id while keeping the display friendly as the list of firm names, but to no avail. Any clues? Thanks :)
Look at the world about you and trust to your own convictions. - Ansel Adams
Meg's World - Blog Photography -
We've recently moved over from ASP 3 for our front end to ASP.NET (using VB.NET sadly). In the past we'd populate our dropdown lists for searches so that the value and the display showed different fields from our database. For example, to allow the user to select a firm in a search, the value might hold the firm id, but the display would be the firm name. Now I'm trying to do the same thing with ASP.NET. Using
cmbFirms.Items.Add(.Item("firm_name"))
for example (where .Item("firm_name")) is in a with block where a recordset is being read out) results in both the value and the display obviously containing the firm's name. Is there a way to give the value of the option selected a different value from the display? I've been trying things like :cmbFirms.SelectedIndex.Equals(.Item("firm_id")) cmbFirms.SelectedValue.Equals(.Item("firm_name"))
andcmbFirms.Items.IndexOf(.Item("firm_id")) cmbFirms.DataValueField = .Item("firm_id")
in desperation to set the index to the firm id while keeping the display friendly as the list of firm names, but to no avail. Any clues? Thanks :)
Look at the world about you and trust to your own convictions. - Ansel Adams
Meg's World - Blog PhotographyYes, you can give the item a different value to the display. However, I'm not too familiar with VB.NET syntax: What does the
.Item(...)
mean. What is the implicit thing that would go infront of the dot? Anyway, You do something like the following (sorry it's in C# syntax)cmbFirms.Items.Add(new ListItem(strDisplayText, strInternalValue));
Does this help?
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
-
Yes, you can give the item a different value to the display. However, I'm not too familiar with VB.NET syntax: What does the
.Item(...)
mean. What is the implicit thing that would go infront of the dot? Anyway, You do something like the following (sorry it's in C# syntax)cmbFirms.Items.Add(new ListItem(strDisplayText, strInternalValue));
Does this help?
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
Thanks Colin, I'm off to try and hack that into VB.NET now ;) A pity about the C# thing - I was outvoted in the department with VB.NET winning out :(( Colin Angus Mackay wrote: What does the .Item(...) mean sorry - I wasn't very clear in my first post - this is already within a
With
block reading a recordset - so that's just a field coming back from SQL :)
Look at the world about you and trust to your own convictions. - Ansel Adams
Meg's World - Blog Photography -
Yes, you can give the item a different value to the display. However, I'm not too familiar with VB.NET syntax: What does the
.Item(...)
mean. What is the implicit thing that would go infront of the dot? Anyway, You do something like the following (sorry it's in C# syntax)cmbFirms.Items.Add(new ListItem(strDisplayText, strInternalValue));
Does this help?
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
:cool: Thanks! This works beautifully:
With rsRisk If .HasRows Then 'if the database has returned something While .Read cmbFirms.Items.Add(New ListItem(.Item("firm_name"), .Item("firm_id"))) End While Else bla bla bla
Onto the next challenge... :-D
Look at the world about you and trust to your own convictions. - Ansel Adams
Meg's World - Blog Photography -
We've recently moved over from ASP 3 for our front end to ASP.NET (using VB.NET sadly). In the past we'd populate our dropdown lists for searches so that the value and the display showed different fields from our database. For example, to allow the user to select a firm in a search, the value might hold the firm id, but the display would be the firm name. Now I'm trying to do the same thing with ASP.NET. Using
cmbFirms.Items.Add(.Item("firm_name"))
for example (where .Item("firm_name")) is in a with block where a recordset is being read out) results in both the value and the display obviously containing the firm's name. Is there a way to give the value of the option selected a different value from the display? I've been trying things like :cmbFirms.SelectedIndex.Equals(.Item("firm_id")) cmbFirms.SelectedValue.Equals(.Item("firm_name"))
andcmbFirms.Items.IndexOf(.Item("firm_id")) cmbFirms.DataValueField = .Item("firm_id")
in desperation to set the index to the firm id while keeping the display friendly as the list of firm names, but to no avail. Any clues? Thanks :)
Look at the world about you and trust to your own convictions. - Ansel Adams
Meg's World - Blog PhotographyJust a suggestion, you probably have a reason for using the recordset. But if you used a dataset with your dropdown list, you could do this:
ddlSelected.DataSource = ds.Tables(0) ddlSelected.DataTextField = ds.Tables(0).Columns("au_lname").ColumnName.ToString() ddlSelected.DataValueField = ds.Tables(0).Columns("au_id").ColumnName.ToString() ddlSelected.DataBind()
:-) Thea -
:cool: Thanks! This works beautifully:
With rsRisk If .HasRows Then 'if the database has returned something While .Read cmbFirms.Items.Add(New ListItem(.Item("firm_name"), .Item("firm_id"))) End While Else bla bla bla
Onto the next challenge... :-D
Look at the world about you and trust to your own convictions. - Ansel Adams
Meg's World - Blog PhotographyGlad to help.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!