Peat
Peat

Reputation: 25

DropDownList selected item keeps selecting the same item ASP.NET c#

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

Upvotes: 1

Views: 2065

Answers (1)

Keval Gangani
Keval Gangani

Reputation: 1328

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;
    }
}

Upvotes: 1

Related Questions