Example of blog calendar function based on calendar in asp.net

Time:2021-7-8

This paper introduces the realization of blog calendar function based on calendar in asp.net. The details are as follows:

How to use the calendar control of. Net to achieve the effect of site calendar in blog? We know that the most important function of site calendar is to show the day when the blog owner wrote the log. Click the date, and you will enter the log list of the selected date,

First of all, we know that the server control in. Net will carry out postback, and the calendar control will carry out a postback on the first day when it is clicked. What we need to do is to change its default link so that it does not trigger the postback event. Second, we need to know which day there is a log. As for whether there is a log, we have to go to the database query.

In calendar, there is a dayrender event, which is triggered when rendering every day. We can start here by defining an array variable:

private int[] arrCurrentDays, arrPreDays, arrNextDays; // The three variables are the current month, the previous month, and the next month
private int intCurrentMonth, intPreMonth, intNextMonth; // Three integer arrays are used to store the date of blogging relative to the month

Then write the following code in the calendar dayrender event:

CalendarDay d = ((DayRenderEventArgs)e).Day;
TableCell c = ((DayRenderEventArgs)e).Cell;
//Initialize the date array with blog in the current month
if (intPreMonth == 0)
{
  intPreMonth = d.Date.Month; //  Note: when the calendar control is initialized, the first month we get is not the current month, but the month of the previous month
  intCurrentMonth = intPreMonth + 1;
  if (intCurrentMonth > 12)
    intCurrentMonth = 1;
  intNextMonth = intCurrentMonth + 1;
  if (intNextMonth > 12)
    intNextMonth = 1;
  arrPreDays = getArrayDay(d.Date.Year, intPreMonth); // Get the date array with blog in the previous month
  arrCurrentDays = getArrayDay(d.Date.Year, intCurrentMonth);// Get the date array with blog in the current month
  arrNextDays = getArrayDay(d.Date.Year, intNextMonth);// Get the date array of blog in the next month
}
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="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" mce_href="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" " + d.Date.Year + "&month=" +
      d.Date.Month + "&day=" + 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="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" mce_href="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" " + d.Date.Year + "&month=" +
      d. Date. Month + "& day =" + d.date. Day + "title = view" + d.date. Day + "daily log >" + 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="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" mce_href="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" " + d.Date.Year + "&month=" +
      d.Date.Month + "&day=" + d.Date.Day + ">" + d.Date.Day + "</a>"));
    }
    j++;
  }

What we notice here is thatgetArrayDay()The method is to query whether there is a log in the current month from the database. What it returns is an array. What I write is as follows:

public int[] getArrayDay(int intYear, int intMonth)
{
  int[] intArray = new int[31];
  //Select the records that meet the requirements from the database and store the date in the array
  string strSql = "select data from test where year(data)=" + intYear +
  " and month(data)=" + intMonth;
  //Call the executereader method in the dbhelperoledb custom class, which returns an oledbdatareader type
  OleDbDataReader dr = dbAccess.DbHelperOleDb.ExecuteReader(strSql);
  int i = 0;
  while (dr.Read())
  {
    if (i == 0)
    {
      intArray[i] = Convert.ToDateTime(dr["data"].ToString()).Day;
      string a=Convert.ToString(intArray[i]);
      i++;
    }
    else if (Convert.ToDateTime(dr["data"].ToString()).Day != intArray[i - 1])
    {
      intArray[i] = Convert.ToDateTime(dr["data"].ToString()).Day;
      i++;
    }
  }
  return intArray;
}

OK, that’s OK. We can see the effect after running it

PS: here are some online date tools for your reference

Online calendar:
http://tools.jb51.net/bianmin/wannianli

Website calendar:
http://tools.jb51.net/bianmin/webwannianli

Online calendar yellow calendar flash version:
http://tools.jb51.net/bianmin/flashwnl

For more information about asp.net, readers who are interested in it can see the special topic of this website:《Summary of the skills of operating JSON with asp.net》、《Summary of asp.net string operation skills》、《Asp.net operation XML skill summary》、《Summary of asp.net file operation skills》、《Summary of asp.net Ajax skills》And《Summary of asp.net cache operation skills》。

I hope that this paper will be helpful to asp.net programming.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]