protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
createcalendar(DateTime.Now.Month.ToString(), DateTime.Now.Year);
}
}
public void createcalendar(string mt, int yr)
{
DataTable dtSource = new DataTable();
dtSource.Columns.Add("Sunday");
dtSource.Columns.Add("Monday");
dtSource.Columns.Add("Tuesday");
dtSource.Columns.Add("Wednesday");
dtSource.Columns.Add("Thursday");
dtSource.Columns.Add("Friday");
dtSource.Columns.Add("Saturday");
int year = yr;
int month = Convert.ToInt16(mt);
for (int y = 1; y <= 12; y++)
{
ddlmonth.Items.Add(new ListItem(y.ToString(), y.ToString()));
}
ddlmonth.SelectedValue = month.ToString();
lblyear.Text = Convert.ToString(year);
int DaysInMonth = DateTime.DaysInMonth(year, month);
int i = 1;
for (int weak = 0; weak <= 5; weak++)
{
DataRow newday = dtSource.NewRow();
for (int day = 1; day <= 7; day++)
{
if (i > DaysInMonth) break;
DateTime dDate = DateTime.ParseExact(i.ToString() + "/" + month.ToString() + "/" + year.ToString(), "d/M/yyyy", null);
DayOfWeek dayWeek = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(dDate);
newday[dayWeek.ToString()] = i.ToString();
i++;
if (dayWeek.ToString() == "Saturday") break;
}
dtSource.Rows.Add(newday);
if (i > DaysInMonth) break;
}
WeekRepeater.DataSource = dtSource;
WeekRepeater.DataBind();
hdnyear.Value = year.ToString();
}
protected void lnkprev_Click(object sender, EventArgs e)
{
createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value) - 1);
}
protected void lnknext_Click(object sender, EventArgs e)
{
createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value) + 1);
}
protected void ddlmonth_SelectedIndexChanged(object sender, EventArgs e)
{
createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value));
}
protected void btnshow_Click(object sender, EventArgs e)
{
createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value));
}
calendar.aspx{
if (!IsPostBack)
{
createcalendar(DateTime.Now.Month.ToString(), DateTime.Now.Year);
}
}
public void createcalendar(string mt, int yr)
{
DataTable dtSource = new DataTable();
dtSource.Columns.Add("Sunday");
dtSource.Columns.Add("Monday");
dtSource.Columns.Add("Tuesday");
dtSource.Columns.Add("Wednesday");
dtSource.Columns.Add("Thursday");
dtSource.Columns.Add("Friday");
dtSource.Columns.Add("Saturday");
int year = yr;
int month = Convert.ToInt16(mt);
for (int y = 1; y <= 12; y++)
{
ddlmonth.Items.Add(new ListItem(y.ToString(), y.ToString()));
}
ddlmonth.SelectedValue = month.ToString();
lblyear.Text = Convert.ToString(year);
int DaysInMonth = DateTime.DaysInMonth(year, month);
int i = 1;
for (int weak = 0; weak <= 5; weak++)
{
DataRow newday = dtSource.NewRow();
for (int day = 1; day <= 7; day++)
{
if (i > DaysInMonth) break;
DateTime dDate = DateTime.ParseExact(i.ToString() + "/" + month.ToString() + "/" + year.ToString(), "d/M/yyyy", null);
DayOfWeek dayWeek = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(dDate);
newday[dayWeek.ToString()] = i.ToString();
i++;
if (dayWeek.ToString() == "Saturday") break;
}
dtSource.Rows.Add(newday);
if (i > DaysInMonth) break;
}
WeekRepeater.DataSource = dtSource;
WeekRepeater.DataBind();
hdnyear.Value = year.ToString();
}
protected void lnkprev_Click(object sender, EventArgs e)
{
createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value) - 1);
}
protected void lnknext_Click(object sender, EventArgs e)
{
createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value) + 1);
}
protected void ddlmonth_SelectedIndexChanged(object sender, EventArgs e)
{
createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value));
}
protected void btnshow_Click(object sender, EventArgs e)
{
createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value));
}
<div id="calbg">
<div id="calcontent">
<table>
<tr>
<td>
<asp:TextBox runat="server" ID="txtdatefrom"></asp:TextBox>
</td>
<td>
<asp:TextBox runat="server" ID="txtdateto"></asp:TextBox>
</td>
<td>
<asp:Button runat="server" ID="btnshow" Text="SHOW" OnClick="btnshow_Click" />
</td>
</tr>
</table>
<table>
<tr>
<td>
<asp:LinkButton runat="server" ID="lnkprev" Text="<" OnClick="lnkprev_Click"></asp:LinkButton>
</td>
<td colspan="5">
<asp:DropDownList runat="server" ID="ddlmonth" OnSelectedIndexChanged="ddlmonth_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
<asp:Label runat="server" ID="lblyear"></asp:Label>
</td>
<td>
<asp:LinkButton runat="server" ID="lnknext" Text=">" OnClick="lnknext_Click"></asp:LinkButton>
</td>
</tr>
</table>
<asp:Repeater ID="WeekRepeater" runat="server" OnItemDataBound="WeekRepeater_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<th>Su
</th>
<th>Mo
</th>
<th>Tu
</th>
<th>Wed
</th>
<th>Thu
</th>
<th>Fr
</th>
<th>Sa
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label1" Text='<%# Eval("Sunday") %>' />
</td>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label2" Text='<%# Eval("Monday") %>' />
</td>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label3" Text='<%# Eval("Tuesday") %>' />
</td>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label4" Text='<%# Eval("Wednesday") %>' />
</td>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label5" Text='<%# Eval("Thursday") %>' />
</td>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label6" Text='<%# Eval("Friday") %>' />
</td>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label7" Text='<%# Eval("Saturday") %>' />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:HiddenField runat="server" ID="hdnyear" />
</div>
</div>