介绍下如何在
SharePoint 部署自定义WebControl. 如下图,我们需要在左下方加一个 自定义控件个人活动日历。
新建一个 Class 继承 System.Web.UI.WebControls.WebControl 源码如下:
CustomCalendar
public class CustomCalendar : System.Web.UI.WebControls.WebControl {
Literal redire = new Literal ();
Calendar cal;
private string _listName = Command.ClassListName;
public string ListName {
get { return _listName; }
set { _listName = value; }
}
private string _viewName = Command.ClassListView;
public string ViewName {
get { return _viewName; }
set { _viewName = value; }
}
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public List<SPListItem> EventList { get; set; }
private bool setMonth = false;
protected override void OnInit ( EventArgs e ) {
this.EnsureChildControls ();
this.Load += new EventHandler ( Calendar_Load );
base.OnInit ( e );
}
void Calendar_Load ( object sender, EventArgs e ) {
}
protected override void CreateChildControls () {
cal = new System.Web.UI.WebControls.Calendar ();
this.cal.ID = "Calendar";
this.cal.CssClass = "Calendar_Main";
this.cal.BorderWidth = Unit.Pixel ( 0 );
this.cal.BorderStyle = BorderStyle.None;
this.cal.TitleStyle.CssClass = "Calenader_Header";
this.cal.OtherMonthDayStyle.CssClass = "Calendar_IsOtherMonth";
this.cal.SelectedDayStyle.CssClass = "Calendar_SelectedDay";
this.cal.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler ( OnDayRender );
this.cal.SelectionChanged += new EventHandler ( this.cal_SelectionChanged );
this.Controls.Add ( cal );
this.Controls.Add ( redire );
}
protected void cal_SelectionChanged ( object sender, EventArgs e ) {
System.Web.UI.WebControls.Calendar sendCal = ( System.Web.UI.WebControls.Calendar ) sender;
RenderEventsByDate ( sendCal.SelectedDate );
}
protected void RenderEventsByDate ( DateTime selectedDate ) {
string url = SPContext.Current.Site.AllWebs [Command.TSWebName].Url + "/Lists/" + ListName.Replace ( ".", "" ) + "/" + ViewName + ".aspx?CalendarDate=" +
string.Format ( "{0:00}", selectedDate.Day ) + Page.Server.UrlEncode ( "/" ) + string.Format ( "{0:00}", selectedDate.Month ) + Page.Server.UrlEncode ( "/" ) +
selectedDate.Year.ToString () + "&CalendarPeriod=day";
redire.Text = "<script language='javascript'>window.location='" + url + "'</script>";
}
protected void OnDayRender ( object sender, DayRenderEventArgs e ) {
CalendarDay day = ( ( DayRenderEventArgs ) e ).Day;
TableCell cell = ( ( DayRenderEventArgs ) e ).Cell;
cell.CssClass = "Calendar_Day";
if ( !setMonth && day.IsOtherMonth ) {
StartTime = day.Date.AddDays ( 1 - day.Date.Day ).AddMonths ( 1 );
EndTime = StartTime.AddMonths ( 1 ).AddDays ( -1 );
setMonth = true;
EventList = GetEventList ();
}
if ( day.IsToday && !day.IsOtherMonth ) {
cell.CssClass = "Calendar_IsToday";
} else if ( day.IsOtherMonth ) {
cell.CssClass = "Calendar_IsOtherMonth";
if ( DateTime.Compare ( day.Date, StartTime ) < 0 ) {
DateTime weekend = day.Date.AddDays ( 6 - day.Date.DayOfWeek.GetHashCode () );
if ( weekend.Month == day.Date.Month ) {
cell.Text = " ";
} else {
cell.ForeColor = System.Drawing.Color.Gray;
}
} else if ( DateTime.Compare ( day.Date, StartTime ) > 0 ) {
DateTime weekStart = day.Date.AddDays ( -day.Date.DayOfWeek.GetHashCode () );
if ( weekStart.Month == day.Date.Month ) {
cell.Text = " ";
} else {
cell.ForeColor = System.Drawing.Color.Gray;
}
}
}
DateTime dayStart = day.Date;
DateTime dayEnd = day.Date.AddDays ( 1 ).AddMinutes ( -1 );
if ( EventList.Where ( item =>
( dayStart <= ( DateTime ) item ["EventDate"] && ( DateTime ) item ["EventDate"] <= dayEnd && dayEnd <= ( DateTime ) item ["EndDate"] ) ||
( ( DateTime ) item ["EventDate"] <= dayStart && dayEnd <= ( DateTime ) item ["EndDate"] && ( DateTime ) item ["EndDate"] <= dayEnd ) ||
( ( DateTime ) item ["EventDate"] <= dayStart && dayEnd <= ( DateTime ) item ["EndDate"] ) ||
( dayStart <= ( DateTime ) item ["EventDate"] && ( DateTime ) item ["EndDate"] <= dayEnd )
).Count () > 0 ) {
cell.CssClass = "Calendar_EventDay";
}
}
protected override void Render ( HtmlTextWriter writer ) {
EnsureChildControls ();
redire.RenderControl ( writer );
cal.RenderControl ( writer );
}
private List<SPListItem> GetEventList () {
Dao dao = new Dao ( Page.Request.Url.Authority, Page.Request.Url.Port.ToString () );
return dao.GetEventList ( new CalendarSearchParameter ()
{
EndTime = EndTime,
ListName = ListName,
StartTime = StartTime
} );
}
}
原理很简单,提取Calender List 里面当月的所有Event ,然后以不同颜色表示,最后Render这个Calender 控件出来。
编译好,在webconfig添加<safecontrol>, 然后在page注册这个控件 如图:
使用的时候
新建一个 Class 继承 System.Web.UI.WebControls.WebControl 源码如下:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class CustomCalendar : System.Web.UI.WebControls.WebControl {
Literal redire = new Literal ();
Calendar cal;
private string _listName = Command.ClassListName;
public string ListName {
get { return _listName; }
set { _listName = value; }
}
private string _viewName = Command.ClassListView;
public string ViewName {
get { return _viewName; }
set { _viewName = value; }
}
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public List<SPListItem> EventList { get; set; }
private bool setMonth = false;
protected override void OnInit ( EventArgs e ) {
this.EnsureChildControls ();
this.Load += new EventHandler ( Calendar_Load );
base.OnInit ( e );
}
void Calendar_Load ( object sender, EventArgs e ) {
}
protected override void CreateChildControls () {
cal = new System.Web.UI.WebControls.Calendar ();
this.cal.ID = "Calendar";
this.cal.CssClass = "Calendar_Main";
this.cal.BorderWidth = Unit.Pixel ( 0 );
this.cal.BorderStyle = BorderStyle.None;
this.cal.TitleStyle.CssClass = "Calenader_Header";
this.cal.OtherMonthDayStyle.CssClass = "Calendar_IsOtherMonth";
this.cal.SelectedDayStyle.CssClass = "Calendar_SelectedDay";
this.cal.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler ( OnDayRender );
this.cal.SelectionChanged += new EventHandler ( this.cal_SelectionChanged );
this.Controls.Add ( cal );
this.Controls.Add ( redire );
}
protected void cal_SelectionChanged ( object sender, EventArgs e ) {
System.Web.UI.WebControls.Calendar sendCal = ( System.Web.UI.WebControls.Calendar ) sender;
RenderEventsByDate ( sendCal.SelectedDate );
}
protected void RenderEventsByDate ( DateTime selectedDate ) {
string url = SPContext.Current.Site.AllWebs [Command.TSWebName].Url + "/Lists/" + ListName.Replace ( ".", "" ) + "/" + ViewName + ".aspx?CalendarDate=" +
string.Format ( "{0:00}", selectedDate.Day ) + Page.Server.UrlEncode ( "/" ) + string.Format ( "{0:00}", selectedDate.Month ) + Page.Server.UrlEncode ( "/" ) +
selectedDate.Year.ToString () + "&CalendarPeriod=day";
redire.Text = "<script language='javascript'>window.location='" + url + "'</script>";
}
protected void OnDayRender ( object sender, DayRenderEventArgs e ) {
CalendarDay day = ( ( DayRenderEventArgs ) e ).Day;
TableCell cell = ( ( DayRenderEventArgs ) e ).Cell;
cell.CssClass = "Calendar_Day";
if ( !setMonth && day.IsOtherMonth ) {
StartTime = day.Date.AddDays ( 1 - day.Date.Day ).AddMonths ( 1 );
EndTime = StartTime.AddMonths ( 1 ).AddDays ( -1 );
setMonth = true;
EventList = GetEventList ();
}
if ( day.IsToday && !day.IsOtherMonth ) {
cell.CssClass = "Calendar_IsToday";
} else if ( day.IsOtherMonth ) {
cell.CssClass = "Calendar_IsOtherMonth";
if ( DateTime.Compare ( day.Date, StartTime ) < 0 ) {
DateTime weekend = day.Date.AddDays ( 6 - day.Date.DayOfWeek.GetHashCode () );
if ( weekend.Month == day.Date.Month ) {
cell.Text = " ";
} else {
cell.ForeColor = System.Drawing.Color.Gray;
}
} else if ( DateTime.Compare ( day.Date, StartTime ) > 0 ) {
DateTime weekStart = day.Date.AddDays ( -day.Date.DayOfWeek.GetHashCode () );
if ( weekStart.Month == day.Date.Month ) {
cell.Text = " ";
} else {
cell.ForeColor = System.Drawing.Color.Gray;
}
}
}
DateTime dayStart = day.Date;
DateTime dayEnd = day.Date.AddDays ( 1 ).AddMinutes ( -1 );
if ( EventList.Where ( item =>
( dayStart <= ( DateTime ) item ["EventDate"] && ( DateTime ) item ["EventDate"] <= dayEnd && dayEnd <= ( DateTime ) item ["EndDate"] ) ||
( ( DateTime ) item ["EventDate"] <= dayStart && dayEnd <= ( DateTime ) item ["EndDate"] && ( DateTime ) item ["EndDate"] <= dayEnd ) ||
( ( DateTime ) item ["EventDate"] <= dayStart && dayEnd <= ( DateTime ) item ["EndDate"] ) ||
( dayStart <= ( DateTime ) item ["EventDate"] && ( DateTime ) item ["EndDate"] <= dayEnd )
).Count () > 0 ) {
cell.CssClass = "Calendar_EventDay";
}
}
protected override void Render ( HtmlTextWriter writer ) {
EnsureChildControls ();
redire.RenderControl ( writer );
cal.RenderControl ( writer );
}
private List<SPListItem> GetEventList () {
Dao dao = new Dao ( Page.Request.Url.Authority, Page.Request.Url.Port.ToString () );
return dao.GetEventList ( new CalendarSearchParameter ()
{
EndTime = EndTime,
ListName = ListName,
StartTime = StartTime
} );
}
}
原理很简单,提取Calender List 里面当月的所有Event ,然后以不同颜色表示,最后Render这个Calender 控件出来。
编译好,在webconfig添加<safecontrol>, 然后在page注册这个控件 如图:
![](https://i-blog.csdnimg.cn/blog_migrate/374ddde4f86288993a6e7544c87e8ad5.png)
<%
@ Register TagPrefix
=
"
uc1
"
Namespace
=
"
SharePoint.Panda.Page
"
Assembly
=
"
SharePoint.Panda.Page, Version=1.0.0.0
"
%>
使用的时候
<
uc1:CustomCalendar
runat
="server"
/>
完工后的结果图: