DropDownList选中的项目保持选择相同的项目ASP.NET c#

时间:2015-04-04 19:51:56

标签: c# asp.net drop-down-menu

我在ASP.NET中的Webform上创建了一个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>

这就是我想要做的事情:

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

使用autopostback并将其设置为true。任何帮助赞赏。 泥炭

1 个答案:

答案 0 :(得分:1)

您的下拉列表项目值必须不同。如果为所有项目提供相同的值,则它在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>

修改

我已在上面的代码中更改了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;
    }
}