views:

506

answers:

3

I have a table with 3 coulumns ,ID,NAme and shortname. I am binding this to a ASP.NET dropdonwlist control.I specified DataText Value field as ID ,DataValueField as Name .Now the Dropdown renders the list item properly and I am able to select the ID of the selected Option. I want to have the short name also with the ID.Is there any way to do this. ? ie ; I want to read the ID,SHort name and Name from the selected option of the ASP.NET dropdownlist

Any thoughts ?

+1  A: 
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 the ShortNameWithId, intead of Id. So, this would be databound to the listbox as some sort of collection (of someclass) ie List, IEnumerable

CRice
A: 
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 may 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 have more coulumns in DataValueField also.

James Zhang
A: 
<form id="form1" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="AccessDataSource1"
        DataTextField="Name" DataValueField="UIR" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
    </asp:DropDownList><asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/MyDB.mdb"
        SelectCommand='SELECT [Name], [ID] & chr(13) & [Shortname] as UIR FROM MyTable'></asp:AccessDataSource>
    <br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</form>
James Zhang