Click here to Skip to main content
15,850,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to dynamic calender create in asp.net?
Posted
Comments
Raghuveer Kasyap 8-Aug-13 5:29am    
Could you please elaborate you question.
Tejas B Pancholi 8-Aug-13 5:50am    
How to dynamic current month calendar create in asp.net without using grid view,etc..
we can not use asp.net control.

1 solution

Hello,
Please use the below mention code to create calender like this:

C#
StringBuilder sb = new StringBuilder();
                sb.Append("<table style="border: 2px solid;width:100%;"><tr><th colspan="7" style="text-align: center;height:40px;">" + DateTime.Now.ToString("Y") + "</th></tr><tr><th style="text-align: center;">Su</th><th style="text-align: center;">Mo</th><th style="text-align: center;">Tu</th><th style="text-align: center;">We</th><th style="text-align: center;">Th</th><th style="text-align: center;">Fr</th><th style="text-align: center;">Sa</th></tr>");
                int year = DateTime.Now.Year;
                int month = DateTime.Now.Month;
                int totaldays = DateTime.DaysInMonth(year, month);

                DateTime firstDay = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                DateTime lastDay = new DateTime(DateTime.Today.Year, DateTime.Today.Month, totaldays);

                int firstdate = (int)firstDay.DayOfWeek;
                int lastdate = (int)lastDay.DayOfWeek;

                DateTime PrelastDay = new DateTime(year, month, 1).AddDays(-1);
                int Preday = PrelastDay.Day;

                DateTime NextFirstDay = new DateTime(year, month, 1).AddMonths(1);
                int Nextday = NextFirstDay.Day;

                int counter = 0;

                for (int j = 0; j < firstdate; j++)
                {
                    if (counter == 0)
                    {
                        sb.Append("<tr>");
                    }
                    sb.Append("<td class="PrevAfterMonth">" + (Preday - firstdate + 1) + "</td>");
                    counter += 1;
                    Preday++;
                    if (counter == 7)
                    {
                        sb.Append("</tr>");
                        counter = 0;
                    }
                }

                for (int i = 1; i <= totaldays; i++)
                {
                    if (counter == 0)
                    {
                        sb.Append("<tr>");
                    }

                    counter += 1;

                    DateTime todate = Convert.ToDateTime(i + "/" + month + "/" + year);
                    sb.Append("<td class="CurrentMonth">" + i + "</td>");

                    if (counter == 7)
                    {
                        sb.Append("</tr>");
                        counter = 0;
                    }
                }

                if (lastdate == 0) lastdate += 6;
                else if (lastdate == 1) lastdate += 4;
                else if (lastdate == 2) lastdate += 2;
                else if (lastdate == 3) lastdate = 3;
                else if (lastdate == 4) lastdate -= 2;
                else if (lastdate == 5) lastdate -= 4;
                else if (lastdate == 6) lastdate += 1;

                for (int k = 0; k < lastdate; k++)
                {
                    if (counter == 0)
                    {
                        sb.Append("<tr>");
                    }
                    sb.Append("<td class="PrevAfterMonth">" + Nextday + "</td>");
                    counter += 1;
                    Nextday++;
                    if (counter == 7)
                    {
                        sb.Append("</tr>");
                        counter = 0;
                    }
                }
                sb.Append("<table>");
</table></table>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900