Rahul
Rahul

Reputation: 438

Add next/previous year button to asp calendar control

Need to add a button for next previous year in ASP calendar control dynamically.

How can I add it right next to the existing next and prev controls? Is it possible to do this via dayrender event ?? Please help

Upvotes: 6

Views: 10304

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460138

No, you cannot modify the existing calendar easily. But maybe it's sufficient to ad a table-row above the calendar where you can select the year.

<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>

Here are two methods to populate the year- and month-dropdownlists:

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;
}

You can initialize the lists from Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Populate_MonthList();
        Populate_YearList();
    }
}

and finally, here's the event handler for the SelectedIndexChanged event for both DropDownLists which sets the new Date:

protected void Set_Calendar(object Sender, EventArgs e)
{
    int year = int.Parse(drpCalYear.SelectedValue);
    int month = int.Parse(drpCalMonth.SelectedValue);
    cntCalendar.TodaysDate = new DateTime(year, month, 1);
}

[ tested ]

Inspired by: https://web.archive.org/web/20210304123649/https://www.4guysfromrolla.com/articles/090104-1.aspx (VB)

Upvotes: 2

Pandian
Pandian

Reputation: 9126

Try the below...

<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>

in code behind

**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);

Upvotes: 4

Related Questions