简体   繁体   中英

How to get text value of a selected item in drop down?

i am trying to get text of a value being selected in drop down list but it doesn't display value but '-Select-'

Code:

if (Convert.ToBoolean(ViewState["Response"])) {   
                String InvoiceNo= Convert.ToString(ViewState["InvoiceNo"]);
                String ConductorName = Convert.ToString(DropDownListConductors.SelectedItem.Text);

                Response.Redirect("~/ManageTransport/ConductorSlip.aspx?InvoiceNo=" + InvoiceNo+"&ConductorName="+ConductorName);
            }
        }

DDL:

DropDownListConductors.DataSource = ManageTransport.ManageConductorDevices.GetConductors();
             DropDownListConductors.DataTextField = "Name";
             DropDownListConductors.DataValueField = "ConductorID";
             DropDownListConductors.DataBind();
             DropDownListConductors.Items.Insert(0, new ListItem("-Select-", "0"));

ConductorSlip.aspx

protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            ConductorName = Request.QueryString["ConductorName"].ToString();
        }
    }

.aspx

<tr>
    <td>Conductor Name</td>
    <td><%=ConductorName %></td>
</tr>

Update you code :

String ConductorName = Convert.ToString(DropDownListConductors.SelectedItem.Text);

to

for selecting value

 String ConductorName = DropDownListConductors.SelectedValue;

for selecting text

 String ConductorName = DropDownListConductors.SelectedItem.Text;

For more information click here

I don't think you need to use Conver.ToString() because the Text property is already a String.

See the code below:-

string ConductorName = DropDownListConductors.SelectedItem.Text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM