【问题标题】:How to get text value of a selected item in drop down?如何在下拉列表中获取所选项目的文本值?
【发布时间】:2015-02-24 07:11:27
【问题描述】:

我正在尝试获取下拉列表中选择的值的文本,但它不显示值,而是显示“-Select-”

代码:

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>

【问题讨论】:

标签: c# asp.net drop-down-menu


【解决方案1】:

更新你的代码:

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

用于选择值

 String ConductorName = DropDownListConductors.SelectedValue;

用于选择文本

 String ConductorName = DropDownListConductors.SelectedItem.Text;

欲了解更多信息click here

【讨论】:

  • 不起作用,它给了我在数据库中分配的 int 值
【解决方案2】:

我认为您不需要使用 Conver.ToString(),因为 Text 属性已经是一个字符串。

请看下面的代码:-

string ConductorName = DropDownListConductors.SelectedItem.Text;

【讨论】:

    猜你喜欢
    • 2012-05-23
    • 2023-03-29
    • 2011-04-02
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多