点击下载工程 其中FinalTest.aspx为测试页面
using System;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//*
* 此控件已经完成自身所带的功能 类似时间选择应该属于别的组件所负责的工作职责,而非把所有的
* 功能都堆砌成一个控件,应该模块化 细化。
* 2007-6-4 21:40开始修改控件
*
* 1、跟blog上的旧代码比较被错误更改的的代码2处,
* 2、处理造成脚本错误的代码,
* 3、把时间选择的功能交给用户控件来处理
* 22:20分出去喝了杯凉茶
* 添加2个月份为一行的功能
* 在2005下完成所有的功能,并转化在2003下
*
* 至23:33完成全部功能,并上传到web上
*
*
* */
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Controls
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
public class DateControl : System.Web.UI.WebControls.WebControl, System.Web.UI.INamingContainer, System.Web.UI.IPostBackEventHandler
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//private const int PerRow=3;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int PerRow
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(ViewState["PerRow"]==null)
ViewState["PerRow"]="3";
return Convert.ToInt32(ViewState["PerRow"]);
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ViewState["PerRow"]=value.ToString();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static readonly object ClickKey = new object();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Description("单击日期触发的事件!")]
public event EventHandler Click
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
add
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Events.AddHandler(ClickKey, value);
}
remove
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Events.RemoveHandler(ClickKey, value);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) private static readonly string[] header = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private DateTime startDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return Convert.ToDateTime(this.StartDate + "-01");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// Gets or sets the show date.
/// </summary>
/// <value>The show date.</value>
[Description("格式必须为yyyy-MM一样")]
public string StartDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (ViewState["startdate"] == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
// if (startDate.ToShortDateString() == null)
// startDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
ViewState["startdate"] = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01").ToString("yyyy-MM");
}
return ViewState["startdate"].ToString();
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//if (value == null)
//{
// startDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
//}
//else
//{
// startDate = Convert.ToDateTime(value + "-01");
//}
ViewState["startdate"] = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private DateTime endDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return Convert.ToDateTime(this.EndDate + "-01");
}
}
[Description("格式必须为yyyy-MM一样")]
public string EndDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (ViewState["enddate"] == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//if (endDate.ToShortDateString() == null)
// endDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
ViewState["enddate"] = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01").ToString("yyyy-MM");
}
return ViewState["enddate"].ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//if (value == null)
//{
// endDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
//}
//else
//{
// endDate = Convert.ToDateTime(value + "-01");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//}
ViewState["enddate"] = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Description("预定的日期")]
[BrowsableAttribute(false)]
public string SelectedDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (Page.Request.Form["datacontrolhid"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return Page.Request.Form["datacontrolhid"].ToString();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return string.Empty;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string initDate;
[Description("字符串形式的数据源")]
public string InitDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
initDate = value;
}
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return initDate;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void OnPreRender(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
base.OnPreRender(e);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) 嵌入css和javascript#region 嵌入css和javascript
// string str = "<link href=\"" + Page.ResolveUrl("~/page.css") + "\" type=\"text/css\" rel=\"stylesheet\" />" //间接引用css
string str = "<style type=\"text/css\" rel=\"stylesheet\">" //直接嵌入css
+ ".borderAll {BORDER-RIGHT: #009a00 1px solid; BORDER-TOP: #009a00 1px solid; BORDER-LEFT: #009a00 1px solid; BORDER-BOTTOM: #009a00 1px solid }"
+ ".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; }"
+ "</style>"
// + "<script type=\"text/javascript\" src=\"" + Page.ResolveUrl("~/Fun_JScript.js") + "\"></script>" //间接引用javascript
+ "<script type=\"text/javascript\">" //直接引用javascript
+ "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);"
+ "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;"
+ "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;}"
+ "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]);"
+ "var strDateE = new Date(strDateArrayEnd[0] + \"/\" + strDateArrayEnd[1] + \"/\" + strDateArrayEnd[2]);intDay = (strDateS-strDateE)/(1000*3600*24);return intDay;}"
+ "</script>"
+ "<script type=\"text/javascript\" >"
+ "var AdSeatListArray = null;var oldFocusCell = null;var focusCell = null;"
+"function SETime(){if(document.getElementById('sYear1').value!=null){document.getElementById('')}}"
+ "function document.onselectstart(){return false;}function Add(id,value){Remove(id,value);document.getElementById('datacontrolhid').value=value+';'+document.getElementById('datacontrolhid').value;}"
+ "function Remove(id,value){document.getElementById('datacontrolhid').value=document.getElementById('datacontrolhid').value.replace(value+';','');}"
+ "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);}}"
+ "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\");"
+ "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\";}}}}}"
+ "function selectedList(){AdSeatListArray = new Array();var operateType = parseInt(document.forms[0].selectOperateType.value);var frequency = parseInt(document.forms[0].selectFrequency.value);"
+ "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);}}"
+ "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++)"
+ "{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;"
+ "adSeatObject.frequency = frequency;AdSeatListArray[0] = adSeatObject;}else{var adSeatObject = AdSeatListArray[AdSeatListArray.length - 1];if(better_time(day,adSeatObject.endDate)==1)"
+ "{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;}}}}}}}"
+ "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;"
+ "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;}}"
+ "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++){"
+ "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;}"
+ "function day_onclick(){selectedCells(1);}function day_oncontextmenu(){selectedCells(2);window.event.cancelBubble = true;window.event.returnValue = false;}"
+ "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();"
+ "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;}"
+ "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;}"
+ "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]);"
+ "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)"
+ "{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;"
+ "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;"
+ "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);}}"
+ "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;"
+ "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)}"
+ "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++)"
+ "{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;}}"
+ "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 = \"\";}}}"
+ "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\";}}}"
+ "</script>";
// + "<script type=\"text/javascript\" src=\"" + Page.ResolveUrl("~/page.js") + "\"></script>"; //间接引用javascript
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Page.Header.Controls.Add(new LiteralControl(str));
if (!Page.IsClientScriptBlockRegistered("tdate"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Page.RegisterClientScriptBlock("tdate", str);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void Render(HtmlTextWriter writer)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int j = 0; //用于月份的累加,即从startDate一直加到endDate
DateTime tempDate = new DateTime(); //表示从startDate到endDate之间月份的临时变量
// string sTime = string.Empty;
// string eTime = string.Empty;
writer.Write("<input type='hidden' name='datacontrolhid' id='" + this.UniqueID + "hid'/>");//隐藏域,用于存放客户选中的日期
//writer.Write("<input type='hidden' name='SToSDate' id='SToSDate'>");
//writer.Write("<input type='hidden' name='SToEDate' id='SToEDate'>");
//writer.Write("<a>开始时间:</a><select id=\"sYear1\" onchange=document.getElementById('SToEDate').value=document.getElementById(\"sYear1\").value+\"-\"+>");
//for (int i = 2000; i < 2050; i++)
//{
// writer.Write("<option id=op" + i.ToString() + " value='" + i.ToString() + "'>" + i.ToString() + "</option>");
//}
//writer.Write("</select>年<select id=\"sMonth1\" onchange=document.getElementById('SToEDate').value=\"0\"+document.getElementById(\"sMonth1\").value;>");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//for (int i = 1; i < 13; i++)
//{
// writer.Write("<option id=op2" + i.ToString() + " value='" + i.ToString() + "'>" + i.ToString() + "</option>");
//}
//writer.Write("</select>月");
//writer.Write(" ");
//writer.Write("<a>结束时间:</a><select id=\"sYear2\" onchange=alert(document.getElementById(\"sYear2\").value);>");
//for (int i = 2000; i < 2050; i++)
//{
// writer.Write("<option id=op" + i.ToString() + " value='" + i.ToString() + "'>" + i.ToString() + "</option>");
//}
//writer.Write("</select>年<select id=\"sMonth2\" onchange=alert(document.getElementById(\"sMonth2\").value)>");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//for (int i = 1; i < 13; i++)
//{
// writer.Write("<option id=op2" + i.ToString() + " value='" + i.ToString() + "'>" + i.ToString() + "</option>");
//}
//writer.Write("</select>月");
//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>");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
writer.RenderBeginTag(HtmlTextWriterTag.Table);
int k=0;
do //为每个月画出一张表格
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(k%PerRow==0)
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//为table标签定义属性和样式,注意:是先写出属性,再给出标签
writer.AddAttribute(HtmlTextWriterAttribute.Width, "380px");
writer.AddAttribute(HtmlTextWriterAttribute.Border, "1px");
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Bordercolor, "#cccc99");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
tempDate = startDate.AddMonths(j);
![](https://www.cnblogs.com/Images/OutliningIndicators/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%\">");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.Write("<td colspan=\"7\" align=\"middle\" class=\"borderBottom\" onclick=\"header_onclick()\">"); //结束标记
writer.Write(tempDate.Year + "年" + tempDate.Month + "月"); //计算当月的年月
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
writer.Write("</td>");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//画出星期几
for (int i = 0; i < DateControl.header.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
// writer.RenderBeginTag(HtmlTextWriterTag.Td);
// writer.Write(DateControl.header[i]);
// writer.RenderEndTag();
writer.Write("<td selected=\"false\" align=\"middle\" bgcolor=\"#99cc33\" class=\"borderRightBottom\" onclick=\"week_onclick()\">");
writer.Write(DateControl.header[i]);
writer.Write("</td>");
}
writer.RenderEndTag();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DateTime end = tempDate.AddMonths(1);
int days = ((TimeSpan)end.Subtract(tempDate)).Days; //计算当月的天数
int spaceday = Convert.ToInt32(tempDate.DayOfWeek); //计算当天是周几并转换成数字
writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//以下循环是用于把1号之前的日期用“ ”即空格填充
for (int i = 0; i < spaceday; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.WriteLine(" ");
writer.RenderEndTag();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int t = 0;
//以下循环用于填充日期,并能把周末用红色标记出来,且伴随着事件的响应即颜色的变化。
for (int i = 1; i <= days; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//定义一个变量存放当月的第一天
string tddate = tempDate.ToString("yyyy-MM") + "-" + i.ToString().PadLeft(2, '0');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// writer.Write(Page.GetPostBackEventReference(this,this.StartDate+"-"+i.ToString().PadLeft(2,'0')));
writer.Write("<td align=\"middle\" Occupancy=\"0\" Prearrange=\"0\" Largess=\"0\" title=\"\" onclick=\"day_onclick()\" oncontextmenu=\"day_oncontextmenu()\" day=\"" + tddate + "\" ");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//存放颜色参数
string colour = string.Empty;
//存放类名
string classname = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//如果系统时间之后背景颜色默认为可预定,即绿色,否则为白色,不可预定
if (Convert.ToDateTime(tddate) > System.DateTime.Now)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
colour = "#99ffff";
classname = "styleCan";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
colour = "white";
classname = "styleCannot";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//if语句是根据用户提供的数据源来对日历进行颜色的初始化
//过期无背景颜色(White)(sign:W),已经被预定显示红色背景(sign:R),可以预定显示绿色(sign:G),被选中的显示蓝色(#6495ed)(sign:B)
if (this.InitDate == "" || this.InitDate == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
writer.Write("");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.InitDate = this.InitDate.TrimEnd(';');
string[] dateStrs = null;
dateStrs = this.SplitPage(this.InitDate, ";");
for (int l = 0; l < dateStrs.Length; l++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//如果当天已经被预定,则背景呈现红色
//switch (dateStrs[l].Substring(dateStrs[l].Length - 1))
//{
// case "R":
//if (dateStrs[l].TrimEnd('R') == tddate)
if (dateStrs[l] == tddate)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
colour = "#ffc0cb";
classname = "styleCannot";
}
// break;
// case "G":
// if (dateStrs[l].TrimEnd('G') == tddate)
// {
// colour = "#99ffff";
// classname = "styleCan";
// }
// break;
// default:
// break;
//}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
writer.Write(" style=\"background-color:" + colour + "\" class=\"" + classname + "\" ");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//周末的字体显示红色
if (tempDate.AddDays(i - 1).DayOfWeek == System.DayOfWeek.Saturday || tempDate.AddDays(i - 1).DayOfWeek == System.DayOfWeek.Sunday)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
writer.Write("><font color='red'>" + i.ToString() + "</font></td>");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
writer.Write(">" + i.ToString() + "</td>");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//每到7天换行
if ((i + spaceday) % 7 == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
writer.RenderEndTag();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// //处理月末是周六的情况
// if ((i + spaceday) / 7 != 5)
// {
// writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
// writer.RenderBeginTag(HtmlTextWriterTag.Tr);
// }
// else
// {
// //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
// }
if(i!=days)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//下文备用
t = i;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//每个月的表格有5行和6行之分
if ((spaceday + days) % 7 != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int allcount = spaceday + days > 35 ? 42 : 35;
for (int i = 0; i < allcount - spaceday - days; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(" ");
writer.RenderEndTag();
}
writer.RenderEndTag();
}
writer.Write("</table>");
// else
// {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**///// if ((t + spaceday) / 7 != 5)
//// {
//// writer.RenderEndTag();
//// }
// writer.RenderEndTag();
// writer.Write("</table>");
// }
j++;
writer.RenderEndTag();
k++;
if(k%PerRow==0)
writer.RenderEndTag();
}
while (tempDate < endDate);
if(k%PerRow!=0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for (int i = 0; i < PerRow-k%PerRow; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(" ");
writer.RenderEndTag();
}
writer.RenderEndTag();
}
writer.RenderEndTag();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 把一个字符串以一个分隔符分开
/// </summary>
/// <param name="source">需分割的字符串</param>
/// <param name="split">分割符</param>
/// <returns>字符数组</returns>
private string[] SplitPage(string source, string split)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int len = split.Length;
ArrayList al = new ArrayList();
int start = 0; //开始位置
int j = -1; //匹配索引位置
while (true)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
j = source.IndexOf(split, start);
if (j > -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
al.Add(source.Substring(start, j - start));
int s = j - start;
start = j + len;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
al.Add(source.Substring(start));
break;
}
}
string[] result;
if (al.Count == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
string[] r = new string[1];
r[0] = source;
result = r;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
string[] r = new string[al.Count];
for (int i = 0; i < al.Count; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
r[i] = al[i].ToString();
}
result = r;
}
return result;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//定义onclick事件处理程序
protected virtual void OnClick(StringEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
EventHandler clickEventDelegate = (EventHandler)Events[ClickKey];
if (clickEventDelegate != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
clickEventDelegate(this, e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) IPostBackEventHandler 成员#region IPostBackEventHandler 成员
public void RaisePostBackEvent(string eventArgument)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//throw new Exception("The method or operation is not implemented.");
OnClick(new StringEventArgs(eventArgument));
}
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class StringEventArgs : EventArgs
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
private string argString;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string ArgString
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) get { return argString; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public StringEventArgs(string arg)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.argString = arg;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.argString;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行效果如下图:
|