将下一个/上一年的按钮添加到asp日历控件

时间:2013-01-17 13:12:19

标签: asp.net controls

需要动态地在ASP日历控件中添加下一年的按钮。

如何在现有的next和prev控件旁边添加它?是否可以通过dayrender事件来做到这一点?请帮忙

2 个答案:

答案 0 :(得分:2)

尝试下面的内容......

<asp:Calendar ID="calender1" runat="server" VisibleDate="2012-10-10"></asp:Calendar>
<asp:Button ID="btnMNext" runat="server" Text="Next Month" />
<asp:Button ID="btnMPrev" runat="server" Text="Prev Month" />
<asp:Button ID="btnNextYr" runat="Server" Text="Next Year" />
<asp:Button ID="btnPrevYr" runat="server" Text="Prev Year" />
<br />
<asp:TextBox ID="txtcur" runat="server"></asp:TextBox>

中的代码

**btnMNext_Click**
String month;
month = calender1.SelectMonthText();
txtcur.Text = calender1.SelectedDate.ToShortDateString();
calender1.VisibleDate = calender1.VisibleDate.AddMonths(1);

**btnPrevYr_Click**
    calender1.VisibleDate = calender1.VisibleDate.AddYears(-1);


**btnNextYr_Click**
    calender1.VisibleDate = calender1.VisibleDate.AddYears(1);
End Sub

**btnMPrev_Click**
    calender1.VisibleDate = calender1.VisibleDate.AddMonths(-1);

答案 1 :(得分:2)

不,您无法轻松修改现有日历。但也许只需在日历上方添加一个表格行即可选择年份。

<table>
    <tr>
        <td>
            <asp:DropDownList id="drpCalMonth" Runat="Server" OnSelectedIndexChanged="Set_Calendar" AutoPostBack="true"></asp:DropDownList>
            <asp:DropDownList id="drpCalYear" Runat="Server" OnSelectedIndexChanged="Set_Calendar" AutoPostBack="true"></asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Calendar id="cntCalendar" Runat="Server" Width="100%" />
        </td>
    </tr>
</table>

以下是填充年度和月份下拉列表的两种方法:

protected void Populate_MonthList()
{
    //Add each month to the list
    var dtf = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
    for(int i=1; i<=12; i++)
        drpCalMonth.Items.Add(new ListItem(dtf.GetMonthName(i), i.ToString()));

    //Make the current month selected item in the list
    drpCalMonth.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
}


protected void Populate_YearList()
{
    //Year list can be changed by changing the lower and upper 
    //limits of the For statement    
    for (int intYear = DateTime.Now.Year - 20; intYear <= DateTime.Now.Year + 20; intYear++)
    {
        drpCalYear.Items.Add(intYear.ToString());
    }

    //Make the current year selected item in the list
    drpCalYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
}

您可以从Page_Load

初始化列表
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Populate_MonthList();
        Populate_YearList();
    }
}

最后,这里是设置新SelectedIndexChanged的{​​{1}} DropDownLists事件的事件处理程序:

Date

[测试]

灵感来自:http://www.4guysfromrolla.com/articles/090104-1.aspx(VB)