簡體   English   中英

DropDownList選定的項目會不斷選擇相同的項目ASP.NET c#

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

我已經在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;
}

使用自動回傳並將其設置為true。 任何幫助表示贊賞。 泥炭

您的下拉列表項值必須不同。 如果為所有項目提供相同的值,則在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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM