ASP.NET Dropdown: Binding More Value for Parameter
I have a table with three columns ID
, Name
and shortname
. I am linking this to the ASP.NET dropdonwlist control. I indicated DataTextField
how ID
, DataValueField
how Name
.
Now the dropdown displays the list item correctly and I can select the ID of the selected option. I want to have a short name and with an id. Is there any way to do this? That is, I want to read the ID, short name and name from the selected ASP.NET dropdown option
Any thoughts?
a source to share
public class SomeClass
{
public int Id { get; set; }
public string ShortName { get; set; }
public string Name { get; set; }
public string ShortNameWithId
{
get
{
return string.Format("{0} - {1}", Id, ShortName);
}
}
}
Now use ShortNameWithId starting with Id. So it will be data binding to the list as some collection (from someclass) i.e. List, IEnumerable
a source to share
Protected Function Fld(ByVal pos As Integer, ByVal mfld As String) As String
Dim i, j, k As Integer
For i = 1 To pos
k = j + 1
j = InStr(k, mfld, Chr(13))
j = IIf(j = 0, Len(mfld) + 1, j)
Next
Return Mid(mfld, k, j - k)
End Function
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = Fld(1, DropDownList1.SelectedItem.Value)
Label2.Text = Fld(2, DropDownList1.SelectedItem.Value)
End Sub
Or you can use:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Fld As Array
Fld = DropDownList1.SelectedItem.Value.Split(Chr(13))
Label1.Text = Fld(0)
Label2.Text = Fld(1)
End Sub
You can also have more columns in the DataValueField.
a source to share