在页面加载的下拉列表中显示所需的值

发布于 2024-09-28 05:18:21 字数 1621 浏览 0 评论 0原文

我正在尝试从数据库中提取月份和年份,并将其显示为在下拉列表中选择的。例如:月份和年份,考虑 2 和 2013。当页面加载时,我希望下拉 expmonth 将 Febraury 和 expyear 显示为 2013 年。 当我尝试下面的代码时,它没有显示 2 和 2013,而是显示默认的 Jan 和 2010。你们认为我哪里做错了,伙计们?

<asp:DropDownList ID="ddExpMonth" runat="server" TabIndex="19" Width="80px" CausesValidation="True">
</asp:DropDownList>
&nbsp;
<asp:DropDownList ID="ddExpYear" runat="server" TabIndex="20" Width="57px" CausesValidation="True">
</asp:DropDownList>

LoadExpDateInfo();
ddExpMonth.SelectedIndex = expmonth-1;
ddExpMonth.SelectedItem.Value = expmonth.ToString();

ddExpYear.SelectedIndex = expyear;
int currentyear = Convert.ToInt16(DateTime.Now.Year);
int diff = expyear - currentyear;
ddExpYear.SelectedItem.Value = diff.ToString();

expyear and expmonth values are retrieved from database.


    private void LoadExpDateInfo()
    {
        string[] arrMonths = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        ListItem item;
        for (int i = 0; i < 12; i++)
        {
            item = new ListItem();
            item.Text = arrMonths[i];
            item.Value = Convert.ToString(i + 1);
            ddExpMonth.Items.Add(item);
        }

        for (int i = -1; i < 10; i++)
        {
            item = new ListItem();
            item.Text = Convert.ToString(System.DateTime.Now.AddYears(1).AddYears(i).Year);
            item.Value = Convert.ToString(System.DateTime.Now.AddYears(1).AddYears(i).Year);
            ddExpYear.Items.Add(item);
        }
    }

感谢你们所有的帮助。我很感激你的时间:)

I am trying to extract month and year from database and show it as selected in the dropdown. Ex: Month and year, consider 2 and 2013. I want the dropdown expmonth to show Febraury and expyear as 2013 when the page is loaded.
When i tried the below code, it did not show me 2 and 2013 instead showed Jan and 2010 which is default. Where do you think i am doing wrong, guys??

<asp:DropDownList ID="ddExpMonth" runat="server" TabIndex="19" Width="80px" CausesValidation="True">
</asp:DropDownList>
 
<asp:DropDownList ID="ddExpYear" runat="server" TabIndex="20" Width="57px" CausesValidation="True">
</asp:DropDownList>

LoadExpDateInfo();
ddExpMonth.SelectedIndex = expmonth-1;
ddExpMonth.SelectedItem.Value = expmonth.ToString();

ddExpYear.SelectedIndex = expyear;
int currentyear = Convert.ToInt16(DateTime.Now.Year);
int diff = expyear - currentyear;
ddExpYear.SelectedItem.Value = diff.ToString();

expyear and expmonth values are retrieved from database.


    private void LoadExpDateInfo()
    {
        string[] arrMonths = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        ListItem item;
        for (int i = 0; i < 12; i++)
        {
            item = new ListItem();
            item.Text = arrMonths[i];
            item.Value = Convert.ToString(i + 1);
            ddExpMonth.Items.Add(item);
        }

        for (int i = -1; i < 10; i++)
        {
            item = new ListItem();
            item.Text = Convert.ToString(System.DateTime.Now.AddYears(1).AddYears(i).Year);
            item.Value = Convert.ToString(System.DateTime.Now.AddYears(1).AddYears(i).Year);
            ddExpYear.Items.Add(item);
        }
    }

Thanks for all your help guys. I appreciate your time :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几味少女 2024-10-05 05:18:21

看起来您可能正在设置 ddExpMonth 的所选项目的值,而不是使用以下命令将其设置为所选项目:

ddExpMonth.SelectedItem.Value = expmonth.ToString();

而是尝试像这样设置所选索引:

ddExpYear.SelectedIndex =
 ddExpYear.Items.IndexOf(
 list.Items.FindByValue("the value"));

It looks like you might be setting the value of the ddExpMonth's selected item instead of making that the selected item with this:

ddExpMonth.SelectedItem.Value = expmonth.ToString();

Instead try setting the selected index like this:

ddExpYear.SelectedIndex =
 ddExpYear.Items.IndexOf(
 list.Items.FindByValue("the value"));
把昨日还给我 2024-10-05 05:18:21

只需获取您想要选择的项目的值并执行以下操作:

ddExpYear.SelectedValue = yourValue;

Just get the value of the item you want selected and do this:

ddExpYear.SelectedValue = yourValue;
南城旧梦 2024-10-05 05:18:21

您只需设置所选索引,而不是所选项目的值。尽管我希望您的初始代码也能产生一些效果,但以下内容应该有效。您是否在页面生命周期中尽早设置了所选索引?您可以通过将所选索引设置为常量值(即 3)来测试这一点,并查看它是否默认为四月。

LoadExpDateInfo(); 
ddExpMonth.SelectedIndex = expmonth-1; 

int currentyear = Convert.ToInt16(DateTime.Now.Year); 
int diff = expyear - currentyear;
ddExpYear.SelectedIndex = diff;

You should only have to set the selected index, not the value of the selected item. The following should work, although I'd expect your initial code to have some effect as well. Are you setting the selected index early enough in the page lifecycle? You can test this by just setting the selected index to a constant value (i.e. 3) and see if it defaults to April.

LoadExpDateInfo(); 
ddExpMonth.SelectedIndex = expmonth-1; 

int currentyear = Convert.ToInt16(DateTime.Now.Year); 
int diff = expyear - currentyear;
ddExpYear.SelectedIndex = diff;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文