动态日历创建 [英] Dynamic Calender Create

查看:74
本文介绍了动态日历创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态日历创建在asp.net?

解决方案

您好,

请使用下面提到的代码创建日历这个:



 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; > ;涂< /第><第style = text-align:center; >我们< / 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(年,月);

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(年,月, 1 )。AddDays (-1);
int Preday = PrelastDay.Day;

DateTime NextFirstDay = new DateTime(年,月, 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 < span class =code-string>
> +(Preday - firstdate + 1 )+ < / td>);
counter + = 1 ;
Preday ++;
if (counter == 7
{
sb。追加( < / tr>);
counter = 0 ;
}
}

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

counter + = 1 ;

DateTime todate = Convert.ToDateTime(i + / +月+ / + 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 < span class =code-string>
> + Nextday + < span class =code-string>< / td>);
counter + = 1 ;
Nextday ++;
if (counter == 7
{
sb。追加( < / tr>);
counter = 0 ;
}
}
sb.Append( < table>);
< / 表格 > < / 表格 >


How to dynamic calender create in asp.net?

解决方案

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

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>


这篇关于动态日历创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆