奇怪的asp.net下拉列表选择值未设置

时间:2013-11-26 10:00:16

标签: c# asp.net

我使用以下代码填充下拉列表:

if (ddl_year.Items.Count == 0)
{
    int current_year = System.DateTime.Now.Year;
    for (int i = 1; i < 11; i++)
    {
        ListItem li = new ListItem((current_year - i).ToString(), 
                (current_year - i).ToString());

        ddl_year.Items.Add(li);
    }

    ddl_year.SelectedIndex = 0;
}

然后,我使用i设置所选的值如下:

ddl_year.SelectedValue="2010";

所选值仍为2012;有人能告诉我我做错了什么吗? 奇怪的是,我对另一个下拉列表做了完全相同的事情,该列表获得了2009的选定值,并且该值正确显示。

任何评论都将受到高度赞赏。

提前致谢

有关实际计划的更多详情:

if (!Page.IsPostback)
{

      FillYears(ddl_year_B); // The content of this method is what i desribed ealier in my post.
      FillYears(ddl_year_A);

      if (Session[CurrentCompareYearWithSessionKey] == null)
            Session[CurrentCompareYearWithSessionKey] = Monitor_year - 1;

      ddl_year_B.SelectedValue = Session[CurrentCompareYearWithSessionKey].ToString();//Works as expected

      if (Session[CurrentSelectedYearSessionKey] == null)
            Session[CurrentSelectedYearSessionKey] = Monitor_year;

      ddl_year_A.SelectedValue = Session[CurrentSelectedYearSessionKey].ToString();// Does not work as expected
}

在aspx标记之下:

<td > Year:</td>
                    <td >
                        <asp:DropDownList  ID="ddl_year_A" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_year_A_SelectedIndexChanged">
                        </asp:DropDownList>
                    </td>
                    <td >Compared year:</td>
                    <td >
                        <asp:DropDownList Width="80px" ID="ddl_year_B" runat="server" AutoPostBack="True"  OnSelectedIndexChanged="ddl_year_B_SelectedIndexChanged">
                        </asp:DropDownList>
                    </td>

3 个答案:

答案 0 :(得分:1)

在我的情况下,您的代码正常工作正常。 PL。告诉Monitor_year的初始值是什么?

我按如下方式重建了你的代码,它正在运作:

            if (!Page.IsPostBack)
            {
                int Monitor_year = 2010;

                FillYears(ddl_year_B); // The content of this method is what i desribed ealier in my post.
                FillYears(ddl_year_A);

                if (Session["CurrentCompareYearWithSessionKey"] == null)
                    Session["CurrentCompareYearWithSessionKey"] = Monitor_year - 1;

                ddl_year_B.SelectedValue = Session["CurrentCompareYearWithSessionKey"].ToString();//Works as expected

                if (Session["CurrentSelectedYearSessionKey"] == null)
                    Session["CurrentSelectedYearSessionKey"] = Monitor_year;

                ddl_year_A.SelectedValue = Session["CurrentSelectedYearSessionKey"].ToString();// Does not work as expected
            }



//-----------

        protected void FillYears(DropDownList ddl_year)
        {
            if (ddl_year.Items.Count == 0)
            {
                int current_year = System.DateTime.Now.Year;
                for (int i = 1; i < 11; i++)
                {
                    ListItem li = new ListItem((current_year - i).ToString(), (current_year - i).ToString());

                    ddl_year.Items.Add(li);
                }

                ddl_year.SelectedIndex = 0;
            }
        }

//---------------------  aspx page

        <asp:DropDownList ID="ddl_year_B" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="ddl_year_A" runat="server"></asp:DropDownList>

The value of ddl_year_B and ddl_year_A is 2009 and 2010 respectively

答案 1 :(得分:0)

试试这段代码,它对我来说非常有用。

    int yr = DateTime.Now.Year;
            for (int y = 0; y < 11; y++)
            {
                ddlYear.Items.Add(new ListItem((yr - y).ToString()));
            }
    ddlYear.SelectedValue = "2010";

答案 2 :(得分:-1)

下拉列表的

SelectedValue属性仅返回当前选定的值。这意味着你不能为这个属性设置任何值。(get property only works).try this

ListItem selectedListItem = ddl.Items.FindByValue("2010");

if (selectedListItem != null)
{
    selectedListItem.Selected = true;
};