简体   繁体   English

DropDownList选定的项目会不断选择相同的项目ASP.NET c#

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

I've created a DropDownList on a Webform in ASP.NET. 我已经在ASP.NET中的Webform上创建了一个DropDownList。 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; 这是我的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. 使用自动回传并将其设置为true。 Any help appreciated. 任何帮助表示赞赏。 Peat 泥炭

Your dropdown list item values must be different. 您的下拉列表项值必须不同。 If you provide same value for all item, it doesn't work in asp.net. 如果为所有项目提供相同的值,则在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: 我已经在上面的代码中更改了DropDown控件的所有值,现在您可以按以下逻辑获取DropDownList的选定值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM