Reputation: 970
I have an asp:Listbox with the DataSource set to Logic.Coms
. This is a list object that contains several Company
objects. If Company
has a property of CompName
, how do I set ListBox.DataTextField to display that name without breaking the integrity of click-to-select?
In this instance, selecting an entry in the listbox will reference the Company
object in order to display its properties in another tab.
For reference, this is what I have so far:
logic.createQuery(nameBox.Text, "companyName");
foreach (Objects.Company c in logic.coms)
{
resultsListBox.DataSource = logic.coms;
resultsListBox.DataTextField = //???
}
resultsListBox.DataBind();
Upvotes: 1
Views: 1837
Reputation: 66439
Set the DataTextField
property to the field you want to display:
<asp:ListBox DataTextField="CompName" ... />
From the documentation:
Use the DataTextField and DataValueField properties to specify which field in the data source to bind to the
Text
andValue
properties, respectively, of each list item in the control.
For anyone familiar with desktop development, these are equivalent to DisplayMember
and ValueMember
.
To set it in code-behind (as you've indicated you need to do), give the control an ID:
<asp:ListBox ID="MyListBox" ... />
And set the same property:
MyListBox.DataTextField = "CompName";
Upvotes: 1