calendar| solution Sometimes you want to add System.Web.UI.WebControls.Calendar to the DataList template in the DataList Edit Template entry, so you can change the date attribute by Calendar, just a little , you do not need the user to fill in a fixed format date. But in the DataList System.Web.UI.WebControls.Calendar control, click Calendar, is unable to respond to the Selectdate event. It's just a simple submission, Will not start Itemcreated,itembound,select,edit,update,cancel wait DataList time. (maybe, but I didn't find it) this is the problem that this post wants to solve.
I looked for it earlier in this edition and didn't see anyone talking about it, of course it should be resolved if you use a client control. But I am more stubborn, do not want to use plum blossom rain, I made a server-side control to resolve the Calendar submission form DataList in the parameters ( This is mainly form["__eventargument"] and form["__eventtarget"), and the results are saved in this control so that it can be accessed later.
I put the solution code posted below, I hope that we can make more bricks, give some advice, I also better have improved!:)
Controls that listen on the Calendar control CageCalendar.dll
Using System;
Using System.Web.UI.WebControls;
Using System.Collections.Specialized;
Namespace Cage
{
<summary>
Cagecalendar is used to detect SelectedDate events for client Canlendar by detecting __eventargument and
__eventtarget the two form hidden fields, returning the date selected in the corresponding Canlendar control. This control is primarily used for
The DataList and the calendar in the DataGrid respond to the event's handling, cage hence the name.
</summary>
public class CageCalendar:System.Web.UI.Control
{
Private DateTime Dtsnifftime;
Public Cagecalendar ()
{
}
Saves the resolved date so that the property can be saved in the ViewState
Public DateTime Snifftime
{
Get
{
return dtsnifftime;
}
Set
{
Dtsnifftime = value;
}
}
Returns the corresponding time object by the day offset relative to January 1, 2000
Principle: Detect the form __eventargument and __eventtarget these two form hidden fields, if valid, will parse the parameters passed to the Help class
Return date, if not valid, return to new DateTime ()
NameValueCollection Form:web A form's property set
String Strfindcontrolpath: DataList control path that needs to be searched
String Strcalendarname: The name of the calendar control that needs to be tapped
Public DateTime getdatetime (namevaluecollection form,string strfindcontrolpath,string strcalendarname)
{
if (form["__eventargument"]!= null &&
form["__eventtarget"]!= null &&
form["__eventargument"]!= "" &&
form["__eventtarget"]!= "")
{
Char[] Csplitarray = {': '};
string[] strpath = form["__eventtarget"]. ToString (). Split (Csplitarray);
String strtemppath = form["__eventtarget"]. Substring (0,form["__eventtarget"). LastIndexOf (":"));
Strtemppath = strtemppath.substring (0,strtemppath.lastindexof (":"));
String strtempcalendarname = form["__eventtarget"]. Substring (form["__eventtarget"). LastIndexOf (":") +1);
if (strtempcalendarname!=strcalendarname)//is not the Calendar control you want to find
return new DateTime ();
if (Strtemppath!=strfindcontrolpath)//is not the control path you want to find
return new DateTime ();
if (form["__eventargument"]. Substring (0,1)!= "V")
{
Return GetDateTime (Convert.ToInt32 (form["__eventargument"));
}
Else
{//page
return new DateTime ();
}
}
return new DateTime ();
}
Returns the corresponding time object by the day offset relative to January 1, 2000
Principle: Calculate the corresponding date according to the offset, the day offset of January 1, 2000 is 0, then a positive number, forward is negative
The number of days is every 400 years for a cycle, which is caused by a leap year
A leap year is or is divisible by 400 or divided by 4 but not divisible by 100.
Public DateTime getdatetime (int iPos)
{
int isign = 1;
if (IPos < 0)
{
iSIGN =-1;
IPos = ( -1) *ipos;
}
int YEAR4 = (365*4+1); The average number of days in 4 years
int Year400 = year4*100-3; 400 years of days, cycles
Int[] Year100array ={year4*25,year4*25-1,year4*25-1,year4*25-1};//400 years, number of days per 100 years
Int[] Montharray = {31,28,31,30,31,30,31,31,30,31,30,31}; Average number of days in December
Int[] Yeararray = {366,365,365,365}; The average number of days in 4 years
int iYearBy400 = ipos/year400; Multiples of 400 years
int iyearby400residue = ipos%year400; The remainder of 400 years
int iYearBy100 = 0; Multiples of 100 years
int iyearby100residue = 0; The remainder of 100 years
int iYearBy4 = 0; Multiples of 4 years
int iyearby4residue = 0; The remainder of 4 years
int iYearIn4 = 0; The year of the 4-year period
int iyearresidue = 0; The remainder of the day of the year in which the 4-year period is in place
int iyear=0; Years
int imonth=0; Month
int iday=0; Day
int imonthresidue = 0; Offset of the month
if (iyearby400residue = 0)
{//400 Year
Iyear = isign*400*iyearby400 +;
return new DateTime (iyear,1,1);
}
int iyear100start = (year100array.length) * (1-isign)/2-(1-isign)/2;
int iyear100end = (year100array.length) * (1+isign)/2-(1-isign)/2;
Iyearby100residue = Iyearby400residue;
for (int i=iyear100start; i!=iyear100end;i=i+isign)
{//calculation in which 100 years