Here i am explaining how to bind the events from database in asp Calender.For this we need asp Calender in our Web form.
Create the database as follow
Currently i have my database as practice.here i am adding a table as Events.The events table look like as follow.
In this table i have 2 columns
- one is Events
- one is Date of that events.
Now i am going to bind these events with my calender dates dynamically. For this add a calender control in your asp.net web form.
Click its smart tag for better look and feel of the calender. This will be done as follow.
Increase the height and width of the calender. Go to the .cs page and write the following code in Calender DayRender event.
What is DayRender Event ?
A-
calendar DayRender event occurs when each day is created in the control hierarchy of the calendar control. DayRender event is raised when each date cell in the calendar control is created.- protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
- {
-
- Literal l = new Literal();
- l.Visible = true;
- l.Text = "<br/>";
- e.Cell.Controls.Add(l);
-
- da = new SqlDataAdapter("select * from Events", con);
- DataTable dt = new DataTable();
- da.Fill(dt);
- foreach(DataRow dr in dt.Rows)
- {
- string x = dr[1].ToString();
-
- if (dr[1].ToString() == e.Day.Date.ToString())
- {
- Label lb = new Label();
- lb.Visible = true;
- lb.Text = dr[0].ToString();
-
- string a = lb.Text;
- e.Cell.Controls.Add(lb);
- e.Cell.BackColor = System.Drawing.Color.OrangeRed;
- e.Cell.ToolTip = dr[0].ToString();
- }
- }
- }
Now it gives the result as follow, binding all events from db table to the Calender.