0

I have made a simple dropdownlist with hard-coded Values and Text's as below:

<asp:DropDownList ID="MonthSelectionDrop" runat="server" EnableViewState="true" >
   <asp:ListItem Value="0" Text="--please select--" />
    <asp:ListItem Value="1" Text="January" />
    <asp:ListItem Value="2" Text="February" />
    <asp:ListItem Value="3" Text="March" />
    <asp:ListItem Value="4" Text="April" />
    <asp:ListItem Value="5" Text="May" />
    <asp:ListItem Value="6" Text="June" />
    <asp:ListItem Value="7" Text="July" />
    <asp:ListItem Value="8" Text="August" />
    <asp:ListItem Value="9" Text="September" />
    <asp:ListItem Value="10" Text="October" />
    <asp:ListItem Value="11" Text="November" />
    <asp:ListItem Value="12" Text="December" />
</asp:DropDownList>

After I press a button, it automatically empties a label and then is meant to be filled with the relevant month. I do this using SelectedItem.Text as such:

MonthLabel.Text += MonthSelectionDrop.SelectedItem.Text + " ";

However, that returns the value of the item, rather then the text, unlike I'd expect.

I can't find the solution to this online, but I'm sure somebody knows how I can have my label show as 'January' rather then '1'

Edit: As per request I'll add my Button_Click event her for if it is usefull to others:

protected void SubmitSelect(object sender, EventArgs e)
{
 if(CheckPresence()) //this is a function that return true at all times atm
{
 MonthLabel.Text = "";
 MonthLabel.Text += MonthSelectionDrop.SelectedItem.Text + " ";
 MonthLabel.Text += YearTextBox.Text;
} else{
   //To be implemented
}
}
4

1 回答 1

0

尝试这个:

ASPX:

<asp:DropDownList ID="MonthSelectionDrop" runat="server"></asp:DropDownList><br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /><br />
<asp:Label ID="MonthLabel" runat="server"></asp:Label>

后面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        MonthSelectionDrop.Items.Add(new ListItem("--please select--", "0"));
        MonthSelectionDrop.Items.Add(new ListItem("January", "1"));
        MonthSelectionDrop.Items.Add(new ListItem("February", "2"));
        //and so on
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    MonthLabel.Text = MonthSelectionDrop.SelectedItem.Text;
}
于 2013-09-13T14:10:47.327 回答