The self-made calendar control can be used to DIY things on the calendar. It is related to writing and other topics, such as "1/28 is the New Year"

Source: Internet
Author: User

Interface file Code :
Bytes -----------------------------------------------------------------------------------------------------
<% @ Control Language = "C #" autoeventwireup = "true" codefile = "customer_canlendar.ascx.cs"
Inherits = "customer_canlendar" %>
<Asp: Calendar id = "custcalendar" runat = "server" backcolor = "# ffffcc" bordercolor = "# ffcc66"
Borderwidth = "1px" daynameformat = "shortest" font-names = "verdana" font-size = "8pt"
Forecolor = "#663399" Height = "222px" ondayrender = "custcalendar_dayrender" onselectionchanged = "custcalendar_selectionchanged"
Showgridlines = "true" width = "340px">
<Selecteddaystyle backcolor = "# ccccff" font-bold = "true"/>
<Todaydaystyle backcolor = "# ffcc66" forecolor = "white"/>
<Selectorstyle backcolor = "# ffcc66"/>
<Othermonthdaystyle forecolor = "# cc9966"/>
<Nextprevstyle font-size = "9pt" forecolor = "# ffffcc"/>
<Dayheaderstyle backcolor = "# ffcc66" font-bold = "true" Height = "1px"/>
<Titlestyle backcolor = "#990000" font-bold = "true" font-size = "9pt" forecolor = "# ffffcc"/>
</ASP: Calendar>
Choose ....................................... .................................. <br/>
Month:
<Asp: dropdownlist id = "ddlmonth" runat = "server">
</ASP: dropdownlist>
& Nbsp;
& Nbsp;
& Nbsp; Day: <asp: dropdownlist id = "ddldays" runat = "server">
</ASP: dropdownlist> <br/>
<Asp: textbox id = "txttitle" runat = "server"> </ASP: textbox>
<Asp: textbox id = "txtcontext" runat = "server" Height = "107px" textmode = "multiline"> </ASP: textbox> & nbsp;
<Asp: button id = "btnsave" runat = "server" text = "save" onclick = "btnsave_click"/>

Bytes -----------------------------------------------------------------------------------
CS file code:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. xml;

/// <Summary>
/// Customer_canlendar
/// </Summary>
Public partial class customer_canlendar: system. Web. UI. usercontrol
{
# Region Param

Private const string emptystring = "";

Private const int monthtotal = 12;

Private const int daystotal = 31;

Public String _ schedule = string. empty;

Public int _ month;

Public int _ days;

String [] [] schedules;

Public const string month = "month ";

Public const string days = "days ";

Public const string context = "context ";

# Endregion

# Region Property

Public String schedule
{< br> Get
{< br> return _ schedule;
}< br> set
{< br> _ schedule = value;
}< BR >}

Public int month
{< br> Get
{< br> return _ month;
}< br> set
{< br> _ month = value;
}< BR >}

Public int days
{
Get
{
Return _ days;
}
Set
{
_ Days = value;
}
}

# Endregion

# Region page_load

protected void page_load (Object sender, eventargs e)
{< br> try
{< br> If (! Ispostback)
{< br> monthdatabind ();
daysdatabind ();
createxmlfile ();
setcontext ();
}< BR >}< br> catch {}< BR >}

# Endregion

# Region custcalendar_dayrender

Private void setcontext ()
{
Schedules = new string [daystotal] [];

For (INT n = 0; n <daystotal; n ++)
Schedules [N] = new string [daystotal + 1];

// Set Context
Readxmlfile ();

}

protected void custcalendar_dayrender (Object sender, dayrendereventargs e)
{< br> try
{< br> calendarday _ calendarday = (dayrendereventargs) e ). day;
tablecell _ tablecell = (dayrendereventargs) e ). cell;

If (_ calendarday. isothermonth)
{< br> _ tablecell. controls. clear ();
}< br> else
{< br> string Hol = schedules [_ calendarday. date. month] [_ calendarday. date. day];

If (HOL! = String. Empty)
_ Tablecell. Controls. Add (New literalcontrol ("<br>" + HOL ));

}
}
Catch {}
}

# Endregion

# Region custcalendar_selectionchanged

Protected void custcalendar_selectionchanged (Object sender, eventargs E)
{

}

# Endregion

# Region monthdatabind & daysdatabind

Private void monthdatabind ()
{
For (INT monthcell = 1; monthcell <= monthtotal; monthcell ++)
{
This. ddlmonth. Items. Add (monthcell. tostring ());
}
}

Private void daysdatabind ()
{
For (INT dayscell = 1; dayscell <= daystotal; dayscell ++)
{
This. ddldays. Items. Add (dayscell. tostring ());
}
}

# Endregion

# Region createxmlfile

Private void createxmlfile ()
{
Bool exist = system. Io. file. exists (request. physicalapplicationpath + "schedule. xml ");
If (! Exist)
{
Xmltextwriter xtw = new xmltextwriter (request. physicalapplicationpath + "schedule. xml", system. Text. encoding. Default );

Xtw. Formatting = formatting. indented;

Xtw. writestartdocument (false );

// Result:
Xtw. writecomment ("schedule infomation ");

// Document comment result:
Xtw. writestartelement ("info ");

Xtw. writeendelement ();

Xtw. Flush ();
// Release resources
Xtw. Close ();
}
}

# Endregion

# Region writexmlfile
/// <Summary>
///
/// </Summary>
/// <Param name = "genretext"> segment value </param>
/// <Param name = "monthxml"> month </param>
/// <Param name = "daysxml"> </param>
/// <Param name = "context"> </param>
Private void writexmlfile (string genretext, string monthxml, string daysxml, string context)
{
// Holidays [1] [1] = "New Year's Day ";
Xmldocument xmldoc = new xmldocument ();

// Load XML
Xmldoc. Load (request. physicalapplicationpath + "schedule. xml ");

// Local XML's Root
Xmlnode root = xmldoc. selectsinglenode ("info"); // query <info>

Xmlelement xe1 = xmldoc. createelement ("Schedule ");
Xe1.setattribute ("genre", genretext );

// Write month to XML
Xmlelement xesub1 = xmldoc. createelement (month );
Xesub1.innertext = monthxml;
Xe1.appendchild (xesub1 );

// Write days to XML
Xmlelement xesub2 = xmldoc. createelement (days );
Xesub2.innertext = daysxml;
Xe1.appendchild (xesub2 );

// Write context to XML
Xmlelement xesub3 = xmldoc. createelement (context );
Xesub3.innertext = context;
Xe1.appendchild (xesub3 );

Root. appendchild (xe1 );

// Save to XML's file
Xmldoc. Save (request. physicalapplicationpath + "schedule. xml ");

}

# Endregion

# Region editxmlfile

/// <Summary>
/// If the input element is month, you can modify the corresponding keys value.
/// </Summary>
/// <Param name = "element"> </param>
/// <Param name = "key"> </param>
Private void editxmlfile (string element, string key)
{
Xmldocument xmldoc = new xmldocument ();

// Load XML
Xmldoc. Load (request. physicalapplicationpath + "schedule. xml ");

// Obtain all subnodes of the info Node
Xmlnodelist nodelist = xmldoc. selectsinglenode ("info"). childnodes;

// Traverse all subnodes
Foreach (xmlnode Xn in nodelist)
{
// Convert the subnode type to the xmlelement type
Xmlelement Xe = (xmlelement) xn;

// Continue to obtain all the child nodes of the Xe subnode
Xmlnodelist NLS = Xe. childnodes;

Foreach (xmlnode xno in NLS)
{
// Conversion Type
Xmlelement xeo = (xmlelement) xno;
If (xeo. Name = element)
{
// Modify
Xeo. innertext = key;
Break;
}
}
}

Xmldoc. Save (request. physicalapplicationpath + "schedule. xml ");
}

# Endregion

# Region delxmlfile

Private void delxmlfile (string genrekeys)
{
Xmldocument xmldoc = new xmldocument ();

// Load XML
Xmldoc. Load (request. physicalapplicationpath + "schedule. xml ");

Xmlnodelist xnl = xmldoc. selectsinglenode ("info"). childnodes;

Foreach (xmlnode Xn in xnl)
{
Xmlelement Xe = (xmlelement) xn;
If (Xe. getattribute ("genre") = genrekeys)
{
Xe. removeall ();
}
}
Xmldoc. Save (request. physicalapplicationpath + "schedule. xml ");
}

# Endregion

# Region readxmlfile

Private void readxmlfile ()
{
Xmlnodereader xmlread = NULL;

Try
{
String nodename = "";
String monthreader = "";
String daysreader = "";
String otherreader = "";

Xmldocument xmldoc = new xmldocument ();

// Load the specified XML document
Xmldoc. Load (request. physicalapplicationpath + "schedule. xml ");

// Set the xmlnodereader object to open the XML file
Xmlread = new xmlnodereader (xmldoc );

// Read and display the data in the XML file
While (xmlread. Read ())
{
// Determine the type of the current read Node
Switch (xmlread. nodetype)
{
Case xmlnodetype. element:
Nodename = xmlread. Name;
Break;
Case xmlnodetype. Text:
If (nodename. Equals (month ))
{
Monthreader = xmlread. value;
}
If (nodename. Equals (days ))
{
Daysreader = xmlread. value;
}
If (nodename. Equals (context ))
{
Otherreader = xmlread. value;
}
Break;
}
If (! Emptystring. Equals (monthreader )&&! Emptystring. Equals (daysreader )&&! Emptystring. Equals (otherreader ))
{
Schedules [system. Convert. toint32 (monthreader)] [system. Convert. toint32 (daysreader)] = system. Convert. tostring (otherreader );
Monthreader = emptystring;
Daysreader = emptystring;
Otherreader = emptystring;
}
}
}
Finally
{
// Clear open data streams
If (xmlread! = NULL)
Xmlread. Close ();
}
}

# Endregion

protected void btnsave_click (Object sender, eventargs e)
{< br> If (! Txttitle. text. equals (session ["title"]) & session ["title"] = NULL)
{< br> session ["title"] = txttitle. text;
writexmlfile (session ["title"]. tostring (), ddlmonth. selecteditem. text, ddldays. selecteditem. text, txtcontext. text);
readxmlfile ();
}< BR >}

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.