DropDownList Asp.net的奇怪行为

时间:2013-02-14 05:39:01

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

我的DropDownList Asp.net

中发生了奇怪的行为

我的代码:

                 <asp:DropDownList ID="DDLFromMinutes" runat="server" Width="20%">
                                <asp:ListItem Text="00" Value="00"></asp:ListItem>
                                <asp:ListItem Text="15" Value="15"></asp:ListItem>
                                <asp:ListItem Text="30" Value="30"></asp:ListItem>
                                <asp:ListItem Text="45" Value="45"></asp:ListItem>
                            </asp:DropDownList>

然而,当我运行它时,如果15是选定值,我在DropDownList

中得到它两次

在FireBug中进行调试时会得到以下结果:

<select id="ContentPlaceHolder1_DDLFromMinutes" style="width:20%;" name="ctl00$ContentPlaceHolder1$DDLFromMinutes">
<option value="00" selected="selected">30</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>

代码背后:

string Frominput = seprateFromTime[1];
                string Frommin = Frominput.Substring(0, 2);
                DDLFromMinutes.SelectedItem.Text = Frommin;
                if (DDLFromMinutes.Items.Count > 0)
                {
                    DDLFromMinutes.SelectedIndex = DDLFromMinutes.Items.IndexOf(DDLFromMinutes.Items.FindByText(Frommin));
                }

enter image description here

保存数据时使用DDLFromMinutes.SelectedItem.Text这可能是个问题吗?

2 个答案:

答案 0 :(得分:1)

您需要使用MyDropDown.SelectedValue而不是selectedItem.Text。

详细信息:MSDN

答案 1 :(得分:0)

你想获得下拉列表的价值吗?如果是这样,我认为您需要将Selected Item更改为SelectedValue。