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?

0


a source to share


3 answers


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

+1


a source


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.

0


a source


    drp.Items.Clear();
    foreach (var item in sourceCollection)
    {
        drp.Items.Add(new ListItem(item.ID+ "," + item.Name + "," + item.ShortName, item.ID));
    }

    //read selected text or value like this:

    string txt = drp.SelectedItem.Text;
    string val = drp.SelectedItem.Value;

      

0


a source







All Articles