asp. net example of blog calendar function based on Calendar

  • 2021-10-11 18:00:05
  • OfStack

This article describes the example of asp. net based on Calendar to achieve blog calendar function. Share it for your reference, as follows:

How to use. net Calendar control to achieve the effect of blog site calendar, we know that the most important function of site calendar is to show which day blog owner wrote a log, click on the date, 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 first day in Calendar control will carry out postback once when it is clicked. What we have to do is to change its default link so that it does not trigger postback events. Secondly, we need to know which day there is no log. As for whether there is a log, it is necessary to query the database.

There is an DayRender event in Calendar, which is triggered every 1 day of rendering. We can start from here and first define an array variable:


private int[] arrCurrentDays, arrPreDays, arrNextDays; //3 The variables are the current month and the previous month 1 Month, and next 1 Months 
private int intCurrentMonth, intPreMonth, intNextMonth; //3 An integer array holds the relative month written with blog Date of 

Then write the following code in the DayRender event of Calendar:


CalendarDay d = ((DayRenderEventArgs)e).Day;
TableCell c = ((DayRenderEventArgs)e).Cell;
//  Initializes the current month Blog Date array of 
if (intPreMonth == 0)
{
  intPreMonth = d.Date.Month; //  Note: When the calendar control is initialized, we get the first 1 Month is not the current month, but the previous month 1 Months of 
  intCurrentMonth = intPreMonth + 1;
  if (intCurrentMonth > 12)
    intCurrentMonth = 1;
  intNextMonth = intCurrentMonth + 1;
  if (intNextMonth > 12)
    intNextMonth = 1;
  arrPreDays = getArrayDay(d.Date.Year, intPreMonth); // Before getting 1 Months have blog Date array of 
  arrCurrentDays = getArrayDay(d.Date.Year, intCurrentMonth);// Get the current month blog Date array of 
  arrNextDays = getArrayDay(d.Date.Year, intNextMonth);// Get next month's blog Date array of 
}
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 journal >" + 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 note here is that getArrayDay() The method is to query whether there is a log in the current month from the database. It returns an array. I wrote the following:


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 dates in an array 
  string strSql = "select data from test where year(data)=" + intYear +
  " and month(data)=" + intMonth;
  // Call DbHelperOleDb Custom in the class ExecuteReader Method , What it returns is 1 A 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 it. We can see the effect after running 1.

PS: Here are some online date tools for your reference:

Online perpetual calendar:
http://tools.ofstack.com/bianmin/wannianli

Page perpetual calendar calendar:
http://tools.ofstack.com/bianmin/webwannianli

Online perpetual calendar flash version:
http://tools.ofstack.com/bianmin/flashwnl

More readers interested in asp.net can check the topics of this site: "Summary of asp.net Operating json Skills", "Summary of asp.net String Operating Skills", "Summary of asp.net Operating XML Skills", "Summary of asp File Operating Skills", "Summary of asp ajax Skills" and "Summary of asp. net Cache Operating Skills".

I hope this paper is helpful to everyone's asp. net programming.


Related articles: