如何使用月份名称填充Combobox并且有动态值 [英] How to Fill Combobox with Month Names And There values Dynamically
本文介绍了如何使用月份名称填充Combobox并且有动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我想用所有月名填充Combobox值可以引导或发送片段
Hello,
I want to Fill Combobox with All Month Names With values can u guide or send snippets
推荐答案
作为VJ reddy写道,您可以使用DateTimeFormatInfo
来获取该信息,但是如果您想获得所选月份的值,请使用简单的技巧:
As VJ reddy wrote, you can use theDateTimeFormatInfo
to get that information, but if you want to get value of selected month use simple trick:
int iVal = ComboBox.SelectedIndex + 1; //if you select February, returns 2
[评论]谢谢你,VJ的评论[/ Comment]
[Comment]Thank you, VJ for your comment[/Comment]
假设你有一个函数来绑定你的数据;
Supposed you have a function to bind your data here;
protected void BindMonthList(DropDownList ddl)
{
var monthList = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames.Select((item, index) => new
{
Month = item,
Value = index+1
});
ddl.DataSource = monthList;
ddl.DataTextField = "Month";
ddl.DataValueField = "Value";
ddl.DataBind();
}
DateTimeFormatInfo $的 MonthNames
属性c $ c>可用于获取月份名称列表,如下所示:
TheMonthNames
property ofDateTimeFormatInfo
can be used to get a list of Month names as shown below:
protected void Page_Load(object sender, EventArgs e)
{
var months = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames;
DropDownList1.DataSource = months;
DropDownList1.DataBind();
}
这篇关于如何使用月份名称填充Combobox并且有动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文