• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

Pass selected text field from drop down list? c#

Is there any way to pass in the selected text field from a drop down list?

I need to pass it in as a string value to this method:

Controller.AddImage(userID, ddlAlbum.SelectedTextField, txtImageName.Text);
protected void Page_Load(object sender, EventArgs e) {
            Albums = new DataSet();
            Albums = NAITBook.Controller.GetAllAlbums(userID);
            ddlAlbum.DataSource = Albums.Tables[0];
            ddlAlbum.DataTextField = "Name";
            ddlAlbum.DataValueField = "AlbumID";
            ddlAlbum.DataBind();
}

Open in new window

0
tqrecords
Asked:
tqrecords
  • 3
1 Solution
 
tcullerCommented:
This all depends on the type of objects stored in the drop-down list (I'm assuming you mean a System.Windows.Forms.ComboBox?). Any time you pass an object to a method like this, such as adding it to a ComboBox, it will call the ToString method on each item top obtain how it should be displayed. Some objects have a "friendlier" ToString method than others, so I'll need to know what type of objects are stored within the ComboBox/ListBox, as well as what format you need the String in.

Or, you can just use the SelectedValue property, like so:

Object selectedValue = nameOfComboBox.SelectedValue;
0
 
tqrecordsAuthor Commented:
hey,

thanks for the reply but I figured it out

just changed the ddlAlbum.DataValueField to "Name"
0
 
ajollyCommented:
I will not suggest you to change the DataValueField to "Name"
This is not suggested.

Usually, we keep the DataValue field to be bound with the internal value or id field.
and the DataTextField to the one field which we want to display.

If any moment, you want to get the value/text of the selected item, use following:

for value = ddlAlbum.SelectedValue          or ddlAlbum.SelectedItem.Value
for text = ddlAlbum.SelectedItem.Text

0
 
tqrecordsAuthor Commented:
OMG you know how long I was looking for that!!??

I knew it was so simple yet I could not find it anywhere!

Thanks alot ajolly!
0
 
tqrecordsAuthor Commented:
Thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now