简体   繁体   中英

Dropdown List not returning the correct value

i am trying to retrieve the value on a previously databinded DropDownList like this:

<asp:DropDownList ID="DropDownListReception" runat="server" CssClass="span3 drop-down-reception"
            OnPreRender="DropDownListReception_PreRender" OnSelectedIndexChanged="DropDownListReception_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>

protected void Page_Load(object sender, EventArgs e)
    {
        var receptions = BLLFactory.ReceptionBLL.GetListAll();
        DropDownListReception.DataSource = receptions;
        DropDownListReception.DataBind();
    }

On the DropDown PreRender i am personalizing this DropDown like this:

protected void DropDownListReception_PreRender(object sender, EventArgs e)
    {
        if (DropDownListReception.DataSource != null)
        {
            DropDownListReception.Items.Clear();
            DropDownListReception.Items.Add(new ListItem("-- Select --", "NA"));
            foreach (Reception item in (DropDownListReception.DataSource as IEnumerable))
            {
                DropDownListReception.Items.Add(new ListItem(item.Name + " " + item.Number, item.Id.ToString()));
            }
        }
    }

this is working perfectly, my DropDown loads as it should, my problem is when i try to retrieve the SelectedValue in the SelectedIndexChanged event, it wont return the value as a string but as a type, what i am doing is:

protected void DropDownListReception_SelectedIndexChanged(object sender, EventArgs e)
    {
        //CurrentReception is a string i want to save in ViewState
        //I also tried (sender as DropDownList).SelectedValue
        //Tried DropDownListReception.SelectedValue
        CurrentReception = DropDownListReception.SelectedItem.Value;
    }

but this "DropDownListReception.SelectedItem.Value" will always return "Reception" which is the type of the item, not the id i assigned as the item value in the PreRender event. This also happens if i do this: "DropDownListReception.SelectedItem.Text", this also return "Reception". How can i return the string Value i assigned to the DropDown item?

var CurrentReception = DropDownListReception.SelectedItem as Reception;
string val = CurrentReception.PropertyYouNeed;

DropDownListReception.SelectedItem.Text and DropDownListReception.SelectedItem.Value will return the value of the selection, which is the second term in the ListItem used when you add it to the list. In other words, the problem is with item.Id.ToString(). It returns the type of the object instead of the ID. I'm not sure what your item object actually consists of so I'm not sure what you actually need, but are you sure it's not just item.Id? ToString() is generally the string representation of the object, if item.Id is an int, then ToString should give you the string equivalent of that int... but the fact that it doesn't work suggests item.Id is not actually an int.

I figured it out, i was DataBinding the DropDownList on the PageLoad, which fires before the SelectedIndexChanged event. Since the DropDown does a PostBack when its value changes, the PageLoad was "Recreating" the DropDown and i was losing the changes before getting to the SelectedIndexChanged code.

Thank you all for your answers.:)

I think you need to cast the list item to the type that you stored in it (Reception), then access the property from the Reception object that you want (from your description it sounds like you want the id). Like this:

protected void DropDownListReception_SelectedIndexChanged(object sender, EventArgs e)
{
    //CurrentReception is a string i want to save in ViewState
    CurentReception = ((Reception)DropDownListReception.SelectedItem).Id.ToString();
}

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