ASP.net Program Implementation Blog Calendar

Source: Internet
Author: User
Tags date array thread visual studio
Asp.net| Blog | program | calendar

Effect Chart:

Front Page

<%@ Page language= "C #" codebehind= "WebForm1.aspx.cs" autoeventwireup= "false" inherits= "Blog_calendar. WebForm1 "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<asp:calendar id= "Calendar1" cellpadding= "2" width= "160px" titlestyle-backcolor= "#000000" bordercolor= "#aaaaaa"
Dayheaderstyle-backcolor= "#5e715e" othermonthdaystyle-forecolor= "#cccccc" daynameformat= "full"
runat= "Server" titlestyle-forecolor= "#ffffff" nextprevstyle-forecolor= "#ffffff" cellspacing= "1"
Weekenddaystyle-backcolor= "#eeeeee" dayheaderstyle-forecolor= "#ffffff" selectionmode= "None"
Todaydaystyle-bordercolor= "#5e715e" todaydaystyle-borderwidth= "1" todaydaystyle-font-bold= "true"
Todaydaystyle-forecolor= "#5e715e" >
<todaydaystyle font-bold= "True" borderwidth= "1px" forecolor= "#5E715E" bordercolor= "#5E715E" ></ Todaydaystyle>
<nextprevstyle forecolor= "White" ></NextPrevStyle>
<dayheaderstyle forecolor= "White" backcolor= "#404040" ></DayHeaderStyle>
<titlestyle forecolor= "white" backcolor= "Black" ></TitleStyle>
<weekenddaystyle backcolor= "Silver" ></WeekendDayStyle>
<othermonthdaystyle forecolor= "#CCCCCC" ></OtherMonthDayStyle>
</asp:calendar>
</form>
</body>
</HTML>

Background program:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Threading;
Using System.Globalization;
Using System.Data.SqlClient;

Namespace Blog_calendar
{
<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Calendar Calendar1;
Private int[] arrcurrentdays,arrpredays,arrnextdays; Three variables are the current month, the previous January, and the next one months
private int intcurrentmonth,intpremonth,intnextmonth; Three integer arrays hold relative month date of blog

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
}

private void Calendar1_prerender (object sender, System.EventArgs e)
{
Thread threadcurrent = Thread.CurrentThread;
CultureInfo cinew = (CultureInfo) threadCurrent.CurrentCulture.Clone ();
CiNew.DateTimeFormat.DayNames = new string[]{"Day", "one", "two", "three", "four", "five", "six"};
CiNew.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
Threadcurrent.currentculture = cinew;
}

private void Calendar1_dayrender (object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
The control occurs each day when it is created.
CalendarDay d = ((Dayrendereventargs) e). Day;
TableCell c = ((Dayrendereventargs) e). Cell;
Initializes an array of dates with blogs for the current month
if (intpremonth = 0)
{
Intpremonth = D.date.month; Note: The first month we get when the Calendar control is initialized is not the current month, but the month of the first one months
Response.Write (D.date.month.tostring ());
Intcurrentmonth = intpremonth+1;
if (intcurrentmonth>12)
intcurrentmonth=1;
Intnextmonth = intcurrentmonth+1;
if (Intnextmonth >12)
Intnextmonth = 1;
Arrpredays = Getarrayday (d.date.year,intpremonth); Get an array of dates with blogs for the first one months
Arrcurrentdays = Getarrayday (d.date.year,intcurrentmonth);//Get an array of dates with blogs for the month
Arrnextdays = Getarrayday (d.date.year,intnextmonth);//Get an array of dates for next month's blog
}

int j=0;

if (D.date.month.equals (Intpremonth))
{
while (! arrpredays[j]. Equals (0))
{
if (D.date.day.equals (Arrpredays[j]))
{
C.controls.clear ();
C.controls.add (New LiteralControl ("<a href=webform1.aspx?year=" +d.date.year+ "-" +
d.date.month+ "-" +d.date.day+ ">" +d.date.day+ "</a>");
}
j + +;
}
}
else if (d.date.month.equals (Intcurrentmonth))
{
while (!ARRCURRENTDAYS[J). Equals (0))
{
if (D.date.day.equals (Arrcurrentdays[j]))
{
C.controls.clear ();
C.controls.add (New LiteralControl ("<a href=webform1.aspx?time=" +d.date.year+ "-" +
d.date.month+ "-" +d.date.day+ "title=" "+d.date.year+ '/' +d.date.month+ '/' +d.date.day+" ' > "+d.date.day+" </a > "));
}
j + +;
}
}
else if (d.date.month.equals (Intnextmonth))
{
while (! arrnextdays[j]. Equals (0))
{
if (D.date.day.equals (Arrnextdays[j]))
{
C.controls.clear ();
C.controls.add (New LiteralControl ("<a href=webform1.aspx?year=" +d.date.year+ "-" +
d.date.month+ "-" +d.date.day+ ">" +d.date.day+ "</a>");
}
j + +;
}
}
}

Private int[] Getarrayday (int intyear,int intmonth)
{
int i=0;
int[] Intarray = new INT[31];
Select the required records from the database and store the dates in an array
String myselectquery = "Select OrderDate from Test where year (OrderDate) =" +intyear+
"And month (OrderDate) =" +intmonth;
SqlConnection myconnection=new SqlConnection ("Server=localhost; Uid=sa;pwd=fan;database=test ");
SqlCommand mycommand = new SqlCommand (myselectquery,myconnection);
Myconnection.open ();
SqlDataReader myreader;
myreader = Mycommand.executereader ();
while (Myreader.read ())
{
if (i==0)
{
Intarray[i] = myreader.getdatetime (0). Day;
i++;
}
else if (myreader.getdatetime (0). Day!= Intarray[i-1])
{
Intarray[i] = myreader.getdatetime (0). Day;
i++;
}
}
Myreader.close ();
Myconnection.close ();

return intarray;
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Calendar1.dayrender + = new System.Web.UI.WebControls.DayRenderEventHandler (this. Calendar1_dayrender);
This. Calendar1.prerender + = new System.EventHandler (this. Calendar1_prerender);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.