Reputation: 461
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
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
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