Reputation: 85
I have a TextBox and a Button. When I enter the Month and Year (Example: April, 2013) in the TextBox, and click on the button, a customized calendar for that particular Month should be displayed.
Note: There will be no Saturdays or Sundays in the calendar. Days will only be from Monday to Friday.
It should be a web-based ASP.NET application using C#.
How can I do this customized calendar? Provide a sample code that implements the above functionality.
Upvotes: 1
Views: 19759
Reputation: 1460
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string month = txtMonth.Text;// should be in the format of Jan, Feb, Mar, Apr, etc...
int yearofMonth = Convert.ToInt32(txtYear.Text);
DateTime dateTime = Convert.ToDateTime("01-" + month + "-" + yearofMonth);
DataRow dr;
DataTable dt = new DataTable();
dt.Columns.Add("Monday");
dt.Columns.Add("Tuesday");
dt.Columns.Add("Wednesday");
dt.Columns.Add("Thursday");
dt.Columns.Add("Friday");
dr = dt.NewRow();
for (int i = 0; i < DateTime.DaysInMonth(dateTime.Year, dateTime.Month); i += 1)
{
txtMonth.Text = Convert.ToDateTime(dateTime.AddDays(0)).ToString("dddd");
if (Convert.ToDateTime(dateTime.AddDays(i)).ToString("dddd") == "Monday")
{
dr["Monday"] = i + 1;
}
if (dateTime.AddDays(i).ToString("dddd") == "Tuesday")
{
dr["Tuesday"] = i + 1;
}
if (dateTime.AddDays(i).ToString("dddd") == "Wednesday")
{
dr["Wednesday"] = i + 1;
}
if (dateTime.AddDays(i).ToString("dddd") == "Thursday")
{
dr["Thursday"] = i + 1;
}
if (dateTime.AddDays(i).ToString("dddd") == "Friday")
{
dr["Friday"] = i + 1;
dt.Rows.Add(dr);
dr = dt.NewRow();
continue;
}
if (i == DateTime.DaysInMonth(dateTime.Year, dateTime.Month) - 1)
{
dt.Rows.Add(dr);
dr = dt.NewRow();
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
and
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtMonth" runat="server">Mon</asp:TextBox>
<asp:TextBox ID="txtYear" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
thanks...
Upvotes: 6