我有一个包含3列ID
,Name
和shortname
的表格。我将它绑定到ASP.NET dropdonwlist控件。我将DataTextField
指定为ID
,将DataValueField
指定为Name
。
现在,下拉列表正确呈现列表项,我可以选择所选选项的ID。我想要短名称和ID。有没有办法做到这一点?即我想从ASP.NET下拉列表的选定选项中读取ID,短名称和名称
有什么想法吗?
答案 0 :(得分:1)
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);
}
}
}
现在使用带有Id的短名称,而不是Id。 所以,这将被数据绑定到列表框中作为某种集合(某些类),即List,IEnumerable
答案 1 :(得分:0)
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
或者您可以使用:
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
您还可以在DataValueField中拥有更多列。
答案 2 :(得分:0)
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;