1

I've created a DropDownList on a Webform in ASP.NET. In the browser I want to select an item from the dropdown list and display the text of the item in a label, but no matter what item I select the dropdown keeps selecting the same item.

This is my DropDownList;

    <asp:DropDownList ID="ddlWeek" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlWeek_SelectedIndexChanged" Visible="False">
        <asp:ListItem Value="1">Select a Week</asp:ListItem>
        <asp:ListItem Value="10">June 7 to June 14, 2015</asp:ListItem>
        <asp:ListItem Value="10">June 14 to June 21, 2015</asp:ListItem>
        <asp:ListItem Value="10">June 21 to June 28, 2015</asp:ListItem>
        <asp:ListItem Value="10">June 28 to July 5, 2015</asp:ListItem>
        <asp:ListItem Value="10">July 5 to July 12, 2015</asp:ListItem>
        <asp:ListItem Value="10">July 12 to July 19, 2015</asp:ListItem>
        <asp:ListItem Value="10">July 19 to July 26, 2015</asp:ListItem>
        <asp:ListItem Value="10">July 26 to August 2, 2015</asp:ListItem>
        <asp:ListItem Value="10">August 2 to August 9, 2015</asp:ListItem>
        <asp:ListItem Value="10">August 9 to August 16, 2015</asp:ListItem>
        <asp:ListItem Value="10">August 16 to August 23, 2015</asp:ListItem>
        <asp:ListItem Value="10">August 23 to August 30, 2015</asp:ListItem>

    </asp:DropDownList>

This is what I am trying to do:

protected void ddlWeek_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    lbWeek.Text = "You selected the week of: " + ddlWeek.SelectedItem.Text;
}

Using autopostback and have it set to true. Any help appreciated. Peat

1 Answer 1

1

Your dropdown list item values must be different. If you provide same value for all item, it doesn't work in asp.net.

<asp:DropDownList ID="ddlWeek" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlWeek_SelectedIndexChanged">
            <asp:ListItem Value="1">Select a Week</asp:ListItem>
            <asp:ListItem Value="2:10">June 7 to June 14, 2015</asp:ListItem>
            <asp:ListItem Value="3:10">June 14 to June 21, 2015</asp:ListItem>
            <asp:ListItem Value="4:10">June 21 to June 28, 2015</asp:ListItem>
            <asp:ListItem Value="5:10">June 28 to July 5, 2015</asp:ListItem>
            <asp:ListItem Value="6:10">July 5 to July 12, 2015</asp:ListItem>
            <asp:ListItem Value="7:10">July 12 to July 19, 2015</asp:ListItem>
            <asp:ListItem Value="8:10">July 19 to July 26, 2015</asp:ListItem>
            <asp:ListItem Value="9:10">July 26 to August 2, 2015</asp:ListItem>
            <asp:ListItem Value="10:10">August 2 to August 9, 2015</asp:ListItem>
            <asp:ListItem Value="11:10">August 9 to August 16, 2015</asp:ListItem>
            <asp:ListItem Value="12:10">August 16 to August 23, 2015</asp:ListItem>
            <asp:ListItem Value="13:10">August 23 to August 30, 2015</asp:ListItem>
        </asp:DropDownList>

Edit:

I have changed the all values of DropDown control in above code and now you can get selected value of DropDownList as below logic:

protected void ddlWeek_SelectedIndexChanged(object sender, EventArgs e)
{
    lbWeek.Text = "You selected the week of: " + ddlWeek.SelectedItem.Text + "<br/>";
    lbWeek.Text += "Value: " + ddlWeek.SelectedItem.Value + "<br/>";

    int liStartIndex = ddlWeek.SelectedItem.Value.IndexOf(":") + 1;
    int liLength = ddlWeek.SelectedItem.Value.Length - liStartIndex;
    string lsOriginalValue = string.Empty;

    if (liStartIndex > 1)
    {
        lsOriginalValue = ddlWeek.SelectedItem.Value.Substring(liStartIndex, liLength);
        lbWeek.Text += "Original Value: " + lsOriginalValue;
    }
}
2
  • Dexterity, Thanks, that's solved it. I had a reason for setting the values all equal to 10 - I intended to change the values depending on number of times an item was selected, but should be able to come up with a different way of doing it.
    – Peat
    Apr 6, 2015 at 18:34
  • Hi Peat, I have edited my answer, please review it and let me know if you have any questions. Apr 7, 2015 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.