简体   繁体   中英

Display dropdownlist selection with a label

I am having a heck of a time all I want is this.

label.text = dropdownlist.text

How do I do that with a dropdownlist that is populated with a datasource in the visual studio designer?

I have an update statement that gets a parameter value from dropdownlist.text, why can't I just set the text property of the label the same way? I think I am missing something really simple but I can't find it.

<asp:DropDownList ID="ddlEditFruit" style="Z-INDEX: 159;
        LEFT:780px; POSITION: absolute; TOP: 280px;"
        runat="server" DataSourceID="update" 
        DataTextField="fruit_id" DataValueField="fruit_id" 
        AppendDataBoundItems="True" AutoPostBack="True">
    <asp:ListItem Selected ="True" Text="" Value ="">(Select)</asp:ListItem>
</asp:DropDownList>




private void btnUpdate_Click(object sender, System.EventArgs e)
{
    if (bxStarch.Text.Trim().Length == 0)
    {
        this.lblError.Visible = true;
    }
    else
    {
        this.lblError.Visible = false;
        //string id = ddlEditFruit.SelectedValue.ToString();
        this.lblId.Visible = true;

        this.lblId.Text = ddlEditFruit.SelectedItem.Text;
        edit_fruit();
        reset_dropdowns();
        resest_dropdowns_2();
        fill_grd_fruit_id();
        fill_grd_size();
        fill_grd_progress();
        SetFocus(ddlGrade);
        change_back();
    }
}

in your codebehind, try this:

label.Text = dropdownlist.SelectedItem.Text

SelectedItem will put the label text to some text, for instance "John"

SelectedValue will put the label text to some value, for instance "5".

您可以尝试使用DropDownListSelectedValue

label.Text = dropdownlist.SelectedValue;
label.Text = dropdownlist.SelectedValue

会这样做:)

I fixed it myself. I was trying to change the label value in the wrong click event. Silly error.

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