在页面加载的下拉列表中显示所需的值
我正在尝试从数据库中提取月份和年份,并将其显示为在下拉列表中选择的。例如:月份和年份,考虑 2 和 2013。当页面加载时,我希望下拉 expmonth 将 Febraury 和 expyear 显示为 2013 年。 当我尝试下面的代码时,它没有显示 2 和 2013,而是显示默认的 Jan 和 2010。你们认为我哪里做错了,伙计们?
<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);
}
}
感谢你们所有的帮助。我很感激你的时间:)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您可能正在设置 ddExpMonth 的所选项目的值,而不是使用以下命令将其设置为所选项目:
而是尝试像这样设置所选索引:
It looks like you might be setting the value of the ddExpMonth's selected item instead of making that the selected item with this:
Instead try setting the selected index like this:
只需获取您想要选择的项目的值并执行以下操作:
Just get the value of the item you want selected and do this:
您只需设置所选索引,而不是所选项目的值。尽管我希望您的初始代码也能产生一些效果,但以下内容应该有效。您是否在页面生命周期中尽早设置了所选索引?您可以通过将所选索引设置为常量值(即 3)来测试这一点,并查看它是否默认为四月。
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.