Introduction: In this Article we'll see how to use the Calendar Control in a Webform.
The Calendar Control shows the Current Month Dates and the Dates also of the previous and Next Month.
So,
The Database Data is stored as:
<
In Page_Load
In Calendar1_PreRender
In Calendar1_SelectionChanged
C#SqlConnection mycn;SqlDataAdapter myda;DataSet ds = new DataSet ();DataSet dsSelDate;String strConn;private void Page_Load(object sender, System.EventArgs e){// Put user code to initialize the page herestrConn="Data Source=localhost;uid=sa;pwd=;Initial Catalog=northwind";mycn = new SqlConnection(strConn);myda = new SqlDataAdapter ("Select * FROM EventsTable", mycn);myda.Fill (ds,"Table");}protected void CalendarDRender(object sender ,System.Web.UI.WebControls.DayRenderEventArgs e ) {// If the month is CurrentMonthif (! e.Day.IsOtherMonth ){foreach (DataRow dr in ds.Tables[0].Rows){if ((dr["EventDate"].ToString() != DBNull.Value.ToString())){DateTime dtEvent= (DateTime)dr["EventDate"];if (dtEvent.Equals(e.Day.Date)){e.Cell.BackColor = Color.PaleVioletRed;}}} }//If the month is not CurrentMonth then hide the Dateselse{e.Cell.Text = "";}}private void Calendar1_SelectionChanged(object sender, System.EventArgs e){myda = new SqlDataAdapter("Select * from EventsTable where EventDate='" + Calendar1.SelectedDate.ToString() + "'", mycn);dsSelDate = new DataSet();myda.Fill(dsSelDate, "AllTables");if (dsSelDate.Tables[0].Rows.Count == 0 ){DataGrid1.Visible = false;}else{DataGrid1.Visible = true;DataGrid1.DataSource = dsSelDate;DataGrid1.DataBind (); }}
ColorFul Web DropDown List in ASP.NET
Validating ASP.NET Server Controls
How to do same for AJAX Calendar control. Would be great if you can add that part also.
After the day is found, should I break the foreach loop?
The calendar comes up with the dates showing red But I can't get the datagrid to come visible.
Error : Specified cast is not valid.DateTime dtEvent= (DateTime)dr["EventDate"];
its a very good post & i have used it but few i have 1) when i lcik on the date on the calendar having the event the grid becomes visible false2)when a date having events is clicked only that date event shld be visible3)& when new added the event shld get save on the selected date