从下拉列表中选择月份名称然后在asp.net日历控件中显示月份的方法是什么我尝试了很多但没有成功
附上图片以便更好地理解
您可以在代码中设置日历的VisibleDate
属性以显示适当的月份,例如:
<asp:DropDownList runat="server" ID="ddlMonth" AutoPostBack="true" OnSelectedIndexChanged="SetCalendarVisibleDate">
<asp:ListItem Text="January" Value="1" />
<asp:ListItem Text="February" Value="2" />
<asp:ListItem Text="March" Value="3" />
<asp:ListItem Text="April" Value="4" />
<asp:ListItem Text="May" Value="5" />
<asp:ListItem Text="June" Value="6" />
<asp:ListItem Text="July" Value="7" />
<asp:ListItem Text="August" Value="8" />
<asp:ListItem Text="September" Value="9" />
<asp:ListItem Text="October" Value="10" />
<asp:ListItem Text="November" Value="11" />
<asp:ListItem Text="December" Value="12" />
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlYear" AutoPostBack="true" OnSelectedIndexChanged="SetCalendarVisibleDate">
<asp:ListItem Text="2011" Value="2011" />
<asp:ListItem Text="2012" Value="2012" />
<asp:ListItem Text="2013" Value="2013" />
<asp:ListItem Text="2014" Value="2014" />
<asp:ListItem Text="2015" Value="2015" />
</asp:DropDownList>
<asp:Calendar runat="server" ID="cal1" />
OnSelectedIndexChanged
然后在您的代码隐藏中,我们只需要为 DropDownLists的事件添加以下事件处理程序:
protected void SetCalendarVisibleDate(object sender, EventArgs e)
{
cal1.VisibleDate = new DateTime(int.Parse(ddlYear.SelectedValue),
int.Parse(ddlMonth.SelectedValue),
1);
}
关于该VisibleDate
属性的 MSDN 文章:http: //msdn.microsoft.com/en-us/library/1adzhse7 (v=vs.100).aspx
这是我的样品,希望对您有帮助,谢谢
https://dotblogs.com.tw/mis2000lab/2014/08/06/calendar_postback_20140806
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int myYear = System.DateTime.Now.Year;
for (int i = 0; i < 101; i++)
{
DropDownList1.Items.Add((myYear - i).ToString());
}
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Calendar1.VisibleDate = new DateTime(Convert.ToInt32(DropDownList1.SelectedValue), Convert.ToInt32(DropDownList2.SelectedValue), 1);
}