Access DropDownlist datavaluefield value

  • Thread starter Eric W. Holzapfel
  • Start date
E

Eric W. Holzapfel

Hello Group,
As far as a dropdownlist, is there a way to access both values for
the datatextfield (like a name, etc) AND the value of the datavaluefield
(like a record number, or index etc).

the dropdown list is populated by a query to a datbase table.
loaded with a datareader like below:

Dlist.DataSource = myCommand.ExecuteReader();
Dlist.DataTextField = TextField; (this would be a name, "Jones Inc.")
Dlist.DataValueField = ValueField; ("CustID", the value of which
is a number like 101, etc)
Dlist.DataBind();

I am using c#, and a dropdown component. It show the name just fine,
but I have not yet seen how to get the customer id associated with the
name (the number is a primary key). I can do it by concatenating two
values from the table like "14 Jones Inc." and then dividing the string
at the space char. I would like to have the dropdown show the only
name, and be able to access the customer id as well.

I have not yet seen how (or if) this can be done with a webcontrol and
web form in .net.

Any tips would be helpful,

Thanks,

eric
 
G

Guest

There are three values you can access for a dropdownlist:

ddlControl.SelectedIndex; // int
ddlControl.SelectedValue; // string
ddlControl.SelectedItem.Text; // string

If value of ddl, like CustID, is integer type, you need convert it:

int CustID = Convert.Toint32(ddlControl.SelectedValue);

HTH

Elton Wang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top