Bunion
Bunion

Reputation: 461

Selecting data from a drop down list

I am trying to get two values from a drop down list the Subject Name and the ID. I have the ID set as selected value with the Subject Name being set as the data field.

Is there a way in which i can turn this data field into a text?

 string subjectName = ddlSubjectName.Text;
 string subjectId =Convert.ToString(ddlSubjectName.SelectedValue);

The Code above is putting the selectedvalue of the drop down to both the subjectName and the subjectId as I'm trying to add both these fields into a my sql database

Upvotes: 2

Views: 93

Answers (2)

Rion Williams
Rion Williams

Reputation: 76557

Both the Text and Value properties of your SelectedItem should contain the properties that you are looking for (i.e. the actual value and text associated to it) :

string subjectName = ddlSubjectName.SelectedItem.Text;
string subjectId = ddlSubjectName.SelectedItem.Value;

Upvotes: 1

fubo
fubo

Reputation: 45947

you can acces both items by

string subjectName = ddlSubjectName.SelectedItem.Text;
string subjectId = ddlSubjectName.SelectedItem.Value; // equals ddlSubjectName.SelectedValue

and they're both type string

Upvotes: 7

Related Questions