I have a dropdown list as below:
DropDownList1.DataSource = Students.GetStudents();
DropDownList1.DataBind();
-----------
I have a DataAccess Class as below:
public IEnumerable<StudentEntity> GetStudents()
{
List<StudentsEntity> studentsList = new List<StudentsEntity>();
studentsList = Service.GetList() // some service getting a list of sutdents
return new BindingList<StudentEntity>(studentsList);
}
I have a DataObject Class as below:
public class StudentEntity : IComparable
{
public string fullname { get {return firstName +", "+ lastName;}
public string ID {get; set;}
public string Height {get; set;}
public string Firstname {get; set;}
public string Lastname {get; set;}
public int CompareTo(object obj)
{
StudentEntity entity = (StudentEntity) obj;
return Lastname.CompareTo(entity.Lastname);
}
}
At the UI level – the ‘Students Fullname’ is displayed in the dropdown list, so how can I get the ‘ID’ of the Selected Student from the DropDown List?
Get the selected item from the DropDownList and cast it to an object of the type StudentEntity. Afterwards you can get the ID of that object. Pseudo code:
Edit:
‘hvd’ commented me correctly. Since this is in a webcontext, you’ll have to achieve this a bit different. You can set the DataTextField and the DataValueField of the DropDownList. Bind the ID to the DataValueField and when you get the SelectedItem, get the Value-property and you have the ID.