时间控件最终版

本文介绍了一个自定义的ASP.NET日期选择控件的实现细节,该控件能够展示多个月份的日历,并允许用户通过多种方式选择日期,包括单选、连续选择等。文章详细展示了控件的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 点击下载工程
其中FinalTest.aspx为测试页面

None.gif using  System;
None.gif
using  System.Text;
None.gif
using  System.ComponentModel;
None.gif
using  System.Web.UI;
None.gif
using  System.Collections;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif * 此控件已经完成自身所带的功能 类似时间选择应该属于别的组件所负责的工作职责,而非把所有的
InBlock.gif * 功能都堆砌成一个控件,应该模块化 细化。
InBlock.gif * 2007-6-4 21:40开始修改控件
InBlock.gif * 
InBlock.gif * 1、跟blog上的旧代码比较被错误更改的的代码2处,
InBlock.gif * 2、处理造成脚本错误的代码,
InBlock.gif * 3、把时间选择的功能交给用户控件来处理
InBlock.gif * 22:20分出去喝了杯凉茶
InBlock.gif * 添加2个月份为一行的功能
InBlock.gif * 在2005下完成所有的功能,并转化在2003下
InBlock.gif * 
InBlock.gif * 至23:33完成全部功能,并上传到web上
InBlock.gif * 
InBlock.gif * 
ExpandedBlockEnd.gif * 
*/

None.gif
None.gif
namespace  Controls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class DateControl : System.Web.UI.WebControls.WebControl, System.Web.UI.INamingContainer, System.Web.UI.IPostBackEventHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif       
//private const int PerRow=3;
InBlock.gif

InBlock.gif        
public int PerRow
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(ViewState["PerRow"]==null)
InBlock.gif                    ViewState[
"PerRow"]="3";
InBlock.gif                
return Convert.ToInt32(ViewState["PerRow"]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"PerRow"]=value.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif        
private static readonly object ClickKey = new object();
InBlock.gif
InBlock.gif        [Description(
"单击日期触发的事件!")]
InBlock.gif        
public event EventHandler Click
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            add
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Events.AddHandler(ClickKey, value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            remove
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Events.RemoveHandler(ClickKey, value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private static readonly string[] header = new string[] dot.gif"星期日""星期一""星期二""星期三""星期四""星期五""星期六" };
InBlock.gif
InBlock.gif        
private DateTime startDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Convert.ToDateTime(this.StartDate + "-01");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Gets or sets the show date.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <value>The show date.</value>

InBlock.gif        [Description("格式必须为yyyy-MM一样")]
InBlock.gif        
public string StartDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ViewState["startdate"== null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// if (startDate.ToShortDateString() == null)
InBlock.gif                    
// startDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
InBlock.gif
                    ViewState["startdate"= Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01").ToString("yyyy-MM");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return ViewState["startdate"].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//if (value == null)
InBlock.gif                
//{
InBlock.gif                
//    startDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
InBlock.gif                
//}
InBlock.gif                
//else
InBlock.gif                
//{
InBlock.gif                
//    startDate = Convert.ToDateTime(value + "-01");
InBlock.gif                
//}
InBlock.gif
                ViewState["startdate"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private DateTime endDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Convert.ToDateTime(this.EndDate + "-01");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [Description(
"格式必须为yyyy-MM一样")]
InBlock.gif        
public string EndDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ViewState["enddate"== null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//if (endDate.ToShortDateString() == null)
InBlock.gif                    
//    endDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
InBlock.gif
                    ViewState["enddate"= Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01").ToString("yyyy-MM");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return ViewState["enddate"].ToString();
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//if (value == null)
InBlock.gif                
//{
InBlock.gif                
//    endDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
InBlock.gif                
//}
InBlock.gif                
//else
InBlock.gif                
//{
InBlock.gif                
//    endDate = Convert.ToDateTime(value + "-01");
InBlock.gif
InBlock.gif                
//}
InBlock.gif
                ViewState["enddate"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Description(
"预定的日期")]
InBlock.gif        [BrowsableAttribute(
false)]
InBlock.gif        
public string SelectedDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (Page.Request.Form["datacontrolhid"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return Page.Request.Form["datacontrolhid"].ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return string.Empty;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string initDate;
InBlock.gif        [Description(
"字符串形式的数据源")]
InBlock.gif        
public string InitDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                initDate 
= value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return initDate;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnPreRender(e);
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif            
嵌入css和javascript#region 嵌入css和javascript
InBlock.gif            
//   string str = "<link href=\"" + Page.ResolveUrl("~/page.css") + "\" type=\"text/css\" rel=\"stylesheet\" />"   //间接引用css
InBlock.gif
            string str = "<style type=\"text/css\" rel=\"stylesheet\">"  //直接嵌入css
InBlock.gif
                + ".borderAll {BORDER-RIGHT: #009a00 1px solid; BORDER-TOP: #009a00 1px solid; BORDER-LEFT: #009a00 1px solid; BORDER-BOTTOM: #009a00 1px solid }"
InBlock.gif                
+ ".borderBottom { cursor:hand;BORDER-BOTTOM: #009a00 1px solid; }.borderRightBottom {BORDER-RIGHT: #009a00 1px solid; BORDER-BOTTOM: #009a00 1px solid;cursor:hand; }.styleCannot { cursor:hand; }.styleCan { cursor:hand;BACKGROUND-COLOR: #99ffff; }"
InBlock.gif                
+ "</style>"
InBlock.gif                
//    + "<script type=\"text/javascript\" src=\"" + Page.ResolveUrl("~/Fun_JScript.js") + "\"></script>" //间接引用javascript
InBlock.gif
                + "<script type=\"text/javascript\">"  //直接引用javascript
InBlock.gif
                + "function isDate(strDate){var strSeparator = \"-\";var strDateArray;var intYear;var intMonth;var intDay;var boolLeapYear;strDateArray = strDate.split(strSeparator);if(strDateArray.length!=3) return false;intYear = parseInt(strDateArray[0],10);intMonth = parseInt(strDateArray[1],10);intDay = parseInt(strDateArray[2],10);"
InBlock.gif                
+ "if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay)) return false;if(intMonth>12||intMonth<1) return false;if((intMonth==1||intMonth==3||intMonth==5||intMonth==7||intMonth==8||intMonth==10||intMonth==12)&&(intDay>31||intDay<1)) return false;"
InBlock.gif                
+ "if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30||intDay<1)) return false;if(intMonth==2){if(intDay<1) return false;boolLeapYear = false;if((intYear%100)==0){if((intYear%400)==0) boolLeapYear = true;}else{if((intYear%4)==0) boolLeapYear = true;}if(boolLeapYear){if(intDay>29) return false;}else{if(intDay>28) return false;}}return true;}"
InBlock.gif                
+ "function better_time(strDateStart,strDateEnd){var strSeparator = \"-\";var strDateArrayStart;var strDateArrayEnd;var intDay;strDateArrayStart = strDateStart.split(strSeparator);strDateArrayEnd = strDateEnd.split(strSeparator);var strDateS = new Date(strDateArrayStart[0] + \"/\" + strDateArrayStart[1] + \"/\" + strDateArrayStart[2]);"
InBlock.gif                
+ "var strDateE = new Date(strDateArrayEnd[0] + \"/\" + strDateArrayEnd[1] + \"/\" + strDateArrayEnd[2]);intDay = (strDateS-strDateE)/(1000*3600*24);return intDay;}"
InBlock.gif                
+ "</script>"
InBlock.gif                
+ "<script type=\"text/javascript\" >"
InBlock.gif                
+ "var AdSeatListArray = null;var oldFocusCell = null;var focusCell = null;"
InBlock.gif                
+"function SETime(){if(document.getElementById('sYear1').value!=null){document.getElementById('')}}"
InBlock.gif                
+ "function document.onselectstart(){return false;}function Add(id,value){Remove(id,value);document.getElementById('datacontrolhid').value=value+';'+document.getElementById('datacontrolhid').value;}"
InBlock.gif                
+ "function Remove(id,value){document.getElementById('datacontrolhid').value=document.getElementById('datacontrolhid').value.replace(value+';','');}"
InBlock.gif                
+ "function showState(){var operateType = parseInt(document.forms[0].selectOperateType.value);var frequency = parseInt(document.forms[0].selectFrequency.value);for(var i=0;i<MonthCount;i++){setTable(i,operateType,frequency);}}"
InBlock.gif                
+ "function setTable(index,type,frequency){var table =document.getElementById(\"TableAdSeatState_\"+index);var rowLength = table.rows.length;var colLength = table.rows(1).cells.length;var today = new Date();var today = today.getFullYear() + \"-\" + (today.getMonth()+1) + \"-\" + today.getDate();for(var row=2;row<rowLength;row++){for(var col = 0; col< colLength; col++){var day = table.rows(row).cells(col).getAttribute(\"day\");"
InBlock.gif                
+ "var Occupancy = table.rows(row).cells(col).getAttribute(\"Occupancy\");if((Occupancy==null)||(better_time(today,day)>0))table.rows(row).cells(col).className = \"\";else{Occupancy = parseInt(Occupancy);if(type==1){Occupancy += parseInt(table.rows(row).cells(col).getAttribute(\"Largess\"));}if(Occupancy + frequency > 100){table.rows(row).cells(col).className = \"styleCannot\";}else{table.rows(row).cells(col).className = \"styleCan\";}}}}}"
InBlock.gif                
+ "function selectedList(){AdSeatListArray = new Array();var operateType = parseInt(document.forms[0].selectOperateType.value);var frequency = parseInt(document.forms[0].selectFrequency.value);"
InBlock.gif                
+ "for(var i=0;i<MonthCount;i++){createResultByIndex(i,operateType,frequency);}if(AdSeatListArray.length == 0){if(window.confirm(\"您还没有选中要预定的日期,继续吗?\"))window.parent.submitAdProject(AdSeatListArray);}else{window.parent.submitAdProject(AdSeatListArray);}}"
InBlock.gif                
+ "function createResultByIndex(index,type,frequency){var table =document.getElementById(\"TableAdSeatState_\"+index);var rowLength = table.rows.length;var colLength = table.rows(1).cells.length;for(var row=2;row<rowLength;row++){for(var col = 0; col< colLength; col++)"
InBlock.gif                
+ "{var td = table.rows(row).cells(col);if(td.className == \"styleCan\"){if(td.style.backgroundColor == \"#00008b\"){var day = td.getAttribute(\"day\");if(AdSeatListArray.length == 0){var adSeatObject = new Object();adSeatObject.adSeatID = currentAdSeatID;adSeatObject.adSeatName = currentAdSeatName;adSeatObject.opType = type;adSeatObject.beginDate = day;adSeatObject.endDate = day;"
InBlock.gif                
+ "adSeatObject.frequency = frequency;AdSeatListArray[0] = adSeatObject;}else{var adSeatObject = AdSeatListArray[AdSeatListArray.length - 1];if(better_time(day,adSeatObject.endDate)==1)"
InBlock.gif                
+ "{adSeatObject.endDate = day;}else{var adSeatObject = new Object();adSeatObject.adSeatID = currentAdSeatID;adSeatObject.adSeatName = currentAdSeatName;adSeatObject.opType = type;adSeatObject.beginDate = day;adSeatObject.endDate = day;adSeatObject.frequency = frequency;AdSeatListArray[AdSeatListArray.length] = adSeatObject;}}}}}}}"
InBlock.gif                
+ "function header_onclick(){var element = event.srcElement;var table = element.parentNode.parentNode.parentNode;var rowLength = table.rows.length;var colLength = table.rows(1).cells.length;"
InBlock.gif                
+ "for(var row=2;row<rowLength;row++){for(var col = 0; col< colLength; col++){var td = table.rows(row).cells(col);if(td.className == \"styleCan\"){if(table.selected){Add('',td.day);td.style.backgroundColor = \"#00008b\";td.style.color = \"white\";}else{Remove('',td.day);td.style.backgroundColor = \"\";td.style.color = \"\";}}}}table.selected = !table.selected;for(var i=0;i<7;i++){ table.rows(1).cells(i).selected = table.selected;}}"
InBlock.gif                
+ "function week_onclick(){var element = event.srcElement;var table = element.parentNode.parentNode.parentNode;var index = element.cellIndex;var rowLength = table.rows.length;for(var row=2;row<rowLength;row++){"
InBlock.gif                
+ "var td = table.rows(row).cells(index);if(td.className == \"styleCan\"){if(element.selected){Add('',td.day);td.style.backgroundColor = \"#00008b\";td.style.color = \"white\";}else{Remove('',td.day);td.style.backgroundColor = \"\";td.style.color = \"\";}}}element.selected = !element.selected;}"
InBlock.gif                
+ "function day_onclick(){selectedCells(1);}function day_oncontextmenu(){selectedCells(2);window.event.cancelBubble = true;window.event.returnValue = false;}"
InBlock.gif                
+ "function selectedCells(buttonIndex){var element = event.srcElement;if(element.tagName == \"FONT\"){element = element.parentNode;}if(event.shiftKey){if(element==focusCell){if(buttonIndex == 1)setCellSelected(element,true);else setCellSelected(element,false);}else{oldFocusCell = focusCell;restoreOldFocusCell();"
InBlock.gif                
+ "if(buttonIndex == 1)setSelectedCells(oldFocusCell,element,true);else setSelectedCells(oldFocusCell,element,false);}}else{restoreOldFocusCell();if(buttonIndex == 1) setCellSelectedAuto(element);else setCellSelected(element,false);}focusCell = element;}"
InBlock.gif                
+ "function restoreOldFocusCell(){var element = event.srcElement;if((focusCell != null)&&(focusCell != element)){focusCell.style.borderRight = \"silver 1px solid\";focusCell.style.borderTop = \"silver 1px solid\";focusCell.style.borderLeft = \"silver 1px solid\";focusCell.style.borderBottom = \"silver 1px solid\";}focusCell = element;}"
InBlock.gif                
+ "function setSelectedCells(oldCell,newCell,selected){var strSeparator = \"_\";var table1 = oldCell.parentNode.parentNode.parentNode;var table2 = newCell.parentNode.parentNode.parentNode;var index1 = parseInt(table1.id.split(strSeparator)[1]);var index2 = parseInt(table2.id.split(strSeparator)[1]);"
InBlock.gif                
+ "if(index1==index2){var rowIndex1 = oldCell.parentNode.rowIndex;var rowIndex2 = newCell.parentNode.rowIndex;if(rowIndex1==rowIndex2){if(oldCell.cellIndex<newCell.cellIndex)setCellSelectedBetween(oldCell,newCell,selected);else setCellSelectedBetween(newCell,oldCell,selected);}else if(rowIndex1<rowIndex2)"
InBlock.gif                
+ "{setCellSelectedBetween(oldCell,newCell,selected);}else{setCellSelectedBetween(newCell,oldCell,selected);}}else{var first = null;var second = null;var firstTable = null;var secondTable = null;var firstCell = null;var secondTable = null;if(index1<index2){first = index1;second = index2;"
InBlock.gif                
+ "firstTable = table1;secondTable = table2;firstCell = oldCell;secondCell = newCell;}else{first = index2;second = index1;firstTable = table2;secondTable = table1;firstCell = newCell;secondCell = oldCell;}var rowLength = firstTable.rows.length;var colLength = firstTable.rows(1).cells.length;"
InBlock.gif                
+ "setCellSelectedBetween(firstCell,firstTable.rows(rowLength-1).cells(colLength-1),selected);for(var i=first+1;i<second;i++){setTableSelected(i,selected);}rowLength = secondCell.parentNode.rowIndex;colLength = secondCell.cellIndex;setCellSelectedBetween(secondTable.rows(2).cells(0),secondCell,selected);}}"
InBlock.gif                
+ "function setCellSelectedBetween(cell1,cell2,selected){var table = cell1.parentNode.parentNode.parentNode;var rowIndex1 = cell1.parentNode.rowIndex;var rowIndex2 = cell2.parentNode.rowIndex;var cellIndex1 = cell1.cellIndex;var cellIndex2 = cell2.cellIndex;var row = rowIndex1;var col = cellIndex1;"
InBlock.gif                
+ "do{setCellSelected(table.rows(row).cells(col),selected);if(col==6){col = 0;row++;}else col++;}while((row!=rowIndex2)||(col!=cellIndex2))setCellSelected(table.rows(row).cells(col),selected)}"
InBlock.gif                
+ "function setTableSelected(index,selected){var table = eval(\"window.document.all.TableAdSeatState_\"  + index);var rowLength = table.rows.length;var colLength = table.rows(1).cells.length;for(var row=2;row<rowLength;row++){for(var col = 0; col< colLength; col++)"
InBlock.gif                
+ "{var td = table.rows(row).cells(col);setCellSelected(td,selected)}}table.selected = true;for(var i=0;i<7;i++){table.rows(1).cells(i).selected = table.selected;}}"
InBlock.gif                
+ "function setCellSelected(td,cellSelected){if(td.className == \"styleCan\"){if(cellSelected){Add('',td.day);td.style.backgroundColor = \"#00008b\";td.style.color = \"white\";}else{Remove('',td.day);td.style.backgroundColor = \"\";td.style.color = \"\";}}}"
InBlock.gif                
+ "function setCellSelectedAuto(td){if(td.className == \"styleCan\"){if(td.style.backgroundColor == \"#00008b\"){Remove('',td.day);td.style.backgroundColor =\"\";td.style.color = \"\";}else{Add('',td.day);td.style.backgroundColor = \"#00008b\";td.style.color = \"white\";}}}"
InBlock.gif                
+ "</script>";
InBlock.gif            
//    + "<script type=\"text/javascript\"  src=\"" + Page.ResolveUrl("~/page.js") + "\"></script>"; //间接引用javascript
ExpandedSubBlockEnd.gif
            #endregion

InBlock.gif
InBlock.gif            
//Page.Header.Controls.Add(new LiteralControl(str));
InBlock.gif
            if (!Page.IsClientScriptBlockRegistered("tdate"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Page.RegisterClientScriptBlock(
"tdate", str);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int j = 0//用于月份的累加,即从startDate一直加到endDate
InBlock.gif
            DateTime tempDate = new DateTime(); //表示从startDate到endDate之间月份的临时变量
InBlock.gif            
// string sTime = string.Empty;
InBlock.gif            
// string eTime = string.Empty;
InBlock.gif
            writer.Write("<input type='hidden' name='datacontrolhid' id='" + this.UniqueID + "hid'/>");//隐藏域,用于存放客户选中的日期
InBlock.gif            
//writer.Write("<input type='hidden' name='SToSDate' id='SToSDate'>");
InBlock.gif            
//writer.Write("<input type='hidden' name='SToEDate' id='SToEDate'>");
InBlock.gif            
InBlock.gif            
//writer.Write("<a>开始时间:</a><select id=\"sYear1\" onchange=document.getElementById('SToEDate').value=document.getElementById(\"sYear1\").value+\"-\"+>");
InBlock.gif            
//for (int i = 2000; i < 2050; i++)
InBlock.gif            
//{
InBlock.gif            
//    writer.Write("<option id=op" + i.ToString() + " value='" + i.ToString() + "'>" + i.ToString() + "</option>");
InBlock.gif            
//}
InBlock.gif            
//writer.Write("</select>年<select id=\"sMonth1\" onchange=document.getElementById('SToEDate').value=\"0\"+document.getElementById(\"sMonth1\").value;>");
InBlock.gif
InBlock.gif            
//for (int i = 1; i < 13; i++)
InBlock.gif            
//{
InBlock.gif            
//    writer.Write("<option id=op2" + i.ToString() + " value='" + i.ToString() + "'>" + i.ToString() + "</option>");
InBlock.gif            
//}
InBlock.gif            
//writer.Write("</select>月");
InBlock.gif            
//writer.Write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
InBlock.gif            
//writer.Write("<a>结束时间:</a><select id=\"sYear2\" onchange=alert(document.getElementById(\"sYear2\").value);>");
InBlock.gif            
//for (int i = 2000; i < 2050; i++)
InBlock.gif            
//{
InBlock.gif            
//    writer.Write("<option id=op" + i.ToString() + " value='" + i.ToString() + "'>" + i.ToString() + "</option>");
InBlock.gif            
//}
InBlock.gif            
//writer.Write("</select>年<select id=\"sMonth2\" onchange=alert(document.getElementById(\"sMonth2\").value)>");
InBlock.gif
InBlock.gif            
//for (int i = 1; i < 13; i++)
InBlock.gif            
//{
InBlock.gif            
//    writer.Write("<option id=op2" + i.ToString() + " value='" + i.ToString() + "'>" + i.ToString() + "</option>");
InBlock.gif            
//}
InBlock.gif            
//writer.Write("</select>月");
InBlock.gif            
//writer.Write("<input type='button' id='btnView' onclick=\"javascript:document.getElementById('SToSDate').value=document.getElementById('sYear1').value+'-'+document.getElementById('sMonth1').value;document.getElementById('SToEDate').value=document.getElementById('sYear2').value+'-'+document.getElementById('sMonth2').value;__doPostBack('"+this.UniqueID+"','');\" value='查看'></input>");
InBlock.gif

InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Table);
InBlock.gif            
int k=0;
InBlock.gif            
do //为每个月画出一张表格
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                
if(k%PerRow==0)
InBlock.gif                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif                writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif
InBlock.gif                
//为table标签定义属性和样式,注意:是先写出属性,再给出标签
InBlock.gif
                writer.AddAttribute(HtmlTextWriterAttribute.Width, "380px");
InBlock.gif                writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"1px");
InBlock.gif                writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, 
"0");
InBlock.gif                writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, 
"0");
InBlock.gif                writer.AddAttribute(HtmlTextWriterAttribute.Bordercolor, 
"#cccc99");
InBlock.gif
InBlock.gif                tempDate 
= startDate.AddMonths(j);
InBlock.gif
InBlock.gif                
//给出标签 
InBlock.gif
                writer.Write("<table id=\"TableAdSeatState_" + j.ToString() + "\" month=\"" + tempDate.ToString("yyyy-MM") + "\" selected=\"false\" cellspacing=\"0\" cellpadding=\"2\" bordercolor=\"silver\" border=\"1\" class=\"borderAll\" width=\"100%\">");
InBlock.gif
InBlock.gif                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif                writer.Write(
"<td colspan=\"7\" align=\"middle\" class=\"borderBottom\"  onclick=\"header_onclick()\">");    //结束标记
InBlock.gif
                writer.Write(tempDate.Year + "" + tempDate.Month + "");  //计算当月的年月
InBlock.gif

InBlock.gif                writer.Write(
"</td>");
InBlock.gif
InBlock.gif                writer.RenderEndTag();
InBlock.gif                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif
InBlock.gif                
//画出星期几
InBlock.gif
                for (int i = 0; i < DateControl.header.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//     writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif                    
//     writer.Write(DateControl.header[i]);
InBlock.gif                    
//     writer.RenderEndTag();
InBlock.gif
                    writer.Write("<td selected=\"false\" align=\"middle\" bgcolor=\"#99cc33\" class=\"borderRightBottom\" onclick=\"week_onclick()\">");
InBlock.gif                    writer.Write(DateControl.header[i]);
InBlock.gif                    writer.Write(
"</td>");
ExpandedSubBlockEnd.gif                }

InBlock.gif                writer.RenderEndTag();
InBlock.gif
InBlock.gif                DateTime end 
= tempDate.AddMonths(1);
InBlock.gif                
int days = ((TimeSpan)end.Subtract(tempDate)).Days;    //计算当月的天数
InBlock.gif
                int spaceday = Convert.ToInt32(tempDate.DayOfWeek);    //计算当天是周几并转换成数字
InBlock.gif
                writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
InBlock.gif                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif
InBlock.gif                
//以下循环是用于把1号之前的日期用“&nbsp;”即空格填充
InBlock.gif
                for (int i = 0; i < spaceday; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif                    writer.WriteLine(
"&nbsp;");
InBlock.gif                    writer.RenderEndTag();
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
int t = 0;
InBlock.gif                
//以下循环用于填充日期,并能把周末用红色标记出来,且伴随着事件的响应即颜色的变化。
InBlock.gif
                for (int i = 1; i <= days; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//定义一个变量存放当月的第一天
InBlock.gif
                    string tddate = tempDate.ToString("yyyy-MM"+ "-" + i.ToString().PadLeft(2'0');
InBlock.gif
InBlock.gif                    
//     writer.Write(Page.GetPostBackEventReference(this,this.StartDate+"-"+i.ToString().PadLeft(2,'0')));
InBlock.gif
                    writer.Write("<td align=\"middle\" Occupancy=\"0\" Prearrange=\"0\" Largess=\"0\" title=\"\" onclick=\"day_onclick()\" oncontextmenu=\"day_oncontextmenu()\" day=\"" + tddate + "\" ");
InBlock.gif
InBlock.gif                    
//存放颜色参数
InBlock.gif
                    string colour = string.Empty;
InBlock.gif                    
//存放类名
InBlock.gif
                    string classname = string.Empty;
InBlock.gif
InBlock.gif                    
//如果系统时间之后背景颜色默认为可预定,即绿色,否则为白色,不可预定
InBlock.gif
                    if (Convert.ToDateTime(tddate) > System.DateTime.Now)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        colour 
= "#99ffff";
InBlock.gif                        classname 
= "styleCan";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        colour 
= "white";
InBlock.gif                        classname 
= "styleCannot";
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
//if语句是根据用户提供的数据源来对日历进行颜色的初始化
InBlock.gif                    
//过期无背景颜色(White)(sign:W),已经被预定显示红色背景(sign:R),可以预定显示绿色(sign:G),被选中的显示蓝色(#6495ed)(sign:B)
InBlock.gif
                    if (this.InitDate == "" || this.InitDate == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        writer.Write(
"");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.InitDate = this.InitDate.TrimEnd(';');
InBlock.gif                        
string[] dateStrs = null;
InBlock.gif                        dateStrs 
= this.SplitPage(this.InitDate, ";");
InBlock.gif                        
for (int l = 0; l < dateStrs.Length; l++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
//如果当天已经被预定,则背景呈现红色
InBlock.gif                            
//switch (dateStrs[l].Substring(dateStrs[l].Length - 1))
InBlock.gif                            
//{
InBlock.gif                            
//    case "R":
InBlock.gif                            
//if (dateStrs[l].TrimEnd('R') == tddate)
InBlock.gif
                            if (dateStrs[l] == tddate)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                colour 
= "#ffc0cb";
InBlock.gif                                classname 
= "styleCannot";
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
//        break;
InBlock.gif                            
//    case "G":
InBlock.gif                            
//        if (dateStrs[l].TrimEnd('G') == tddate)
InBlock.gif                            
//        {
InBlock.gif                            
//            colour = "#99ffff";
InBlock.gif                            
//            classname = "styleCan";
InBlock.gif                            
//        }
InBlock.gif                            
//        break;
InBlock.gif                            
//    default:
InBlock.gif                            
//        break;
InBlock.gif                            
//}
ExpandedSubBlockEnd.gif
                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    writer.Write(
" style=\"background-color:" + colour + "\" class=\"" + classname + "\" ");
InBlock.gif
InBlock.gif                    
//周末的字体显示红色
InBlock.gif
                    if (tempDate.AddDays(i - 1).DayOfWeek == System.DayOfWeek.Saturday || tempDate.AddDays(i - 1).DayOfWeek == System.DayOfWeek.Sunday)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        writer.Write(
"><font color='red'>" + i.ToString() + "</font></td>");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        writer.Write(
">" + i.ToString() + "</td>");
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
//每到7天换行
InBlock.gif
                    if ((i + spaceday) % 7 == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        writer.RenderEndTag();
InBlock.gif
InBlock.gif
//                        //处理月末是周六的情况
InBlock.gif
//                        if ((i + spaceday) / 7 != 5)
InBlock.gif
//                        {
InBlock.gif
//                            writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
InBlock.gif
//                            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif
//                        }
InBlock.gif
//                        else
InBlock.gif
//                        {
InBlock.gif
//                            //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif
//                        }
InBlock.gif
                        if(i!=days)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
//下文备用
InBlock.gif
                    t = i;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
//每个月的表格有5行和6行之分
InBlock.gif
                if ((spaceday + days) % 7 != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int allcount = spaceday + days > 35 ? 42 : 35;
InBlock.gif                    
for (int i = 0; i < allcount - spaceday - days; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif                        writer.Write(
"&nbsp;");
InBlock.gif                        writer.RenderEndTag();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    writer.RenderEndTag();
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                writer.Write(
"</table>");
InBlock.gif
//                else
InBlock.gif
//                {
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**/////                    if ((t + spaceday) / 7 != 5)
InBlock.gif
////                    {
InBlock.gif
////                        writer.RenderEndTag();
ExpandedSubBlockEnd.gif
////                    }

InBlock.gif//                    writer.RenderEndTag();
InBlock.gif
//                    writer.Write("</table>");
InBlock.gif
//                }
InBlock.gif
                j++;
InBlock.gif                writer.RenderEndTag();
InBlock.gif                k
++;
InBlock.gif                
if(k%PerRow==0)
InBlock.gif                    writer.RenderEndTag();
InBlock.gif                
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
while (tempDate < endDate);
InBlock.gif            
if(k%PerRow!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int i = 0; i < PerRow-k%PerRow; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif                    writer.Write(
"&nbsp;");
InBlock.gif                    writer.RenderEndTag();
ExpandedSubBlockEnd.gif                }

InBlock.gif                writer.RenderEndTag();
ExpandedSubBlockEnd.gif            }

InBlock.gif            writer.RenderEndTag();
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 把一个字符串以一个分隔符分开
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="source">需分割的字符串</param>
InBlock.gif        
/// <param name="split">分割符</param>
ExpandedSubBlockEnd.gif        
/// <returns>字符数组</returns>

InBlock.gif        private string[] SplitPage(string source, string split)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int len = split.Length;
InBlock.gif            ArrayList al 
= new ArrayList();
InBlock.gif            
int start = 0//开始位置
InBlock.gif
            int j = -1//匹配索引位置
InBlock.gif
            while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                j 
= source.IndexOf(split, start);
InBlock.gif                
if (j > -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    al.Add(source.Substring(start, j 
- start));
InBlock.gif                    
int s = j - start;
InBlock.gif                    start 
= j + len;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    al.Add(source.Substring(start));
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
string[] result;
InBlock.gif            
if (al.Count == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string[] r = new string[1];
InBlock.gif                r[
0= source;
InBlock.gif                result 
= r;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string[] r = new string[al.Count];
InBlock.gif                
for (int i = 0; i < al.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    r[i] 
= al[i].ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                result 
= r;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//定义onclick事件处理程序
InBlock.gif
        protected virtual void OnClick(StringEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            EventHandler clickEventDelegate 
= (EventHandler)Events[ClickKey];
InBlock.gif            
if (clickEventDelegate != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                clickEventDelegate(
this, e);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IPostBackEventHandler 成员#region IPostBackEventHandler 成员
InBlock.gif        
public void RaisePostBackEvent(string eventArgument)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
InBlock.gif
            OnClick(new StringEventArgs(eventArgument));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class StringEventArgs : EventArgs
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string argString;
InBlock.gif
InBlock.gif        
public string ArgString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn argString; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public StringEventArgs(string arg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.argString = arg;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.argString;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

运行效果如下图:

转载于:https://www.cnblogs.com/deadshot123/archive/2007/06/04/771351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值