01/22 (2): SP : Webpart of Ajax Enabled in SP
- Set SP support Ajax
- Create Webpart control
- Code Sample :
- Deployment :
- Copy .dll to bin folder of share point server
- Modify web.config of folder:
- create dwp file
- Import
- 接下来,就可以在我们的站点上导入这个web part了,打开SharePoint网站,进入您要添加该web part的页面,依次点击“网站操作”->“编辑网页”,在web部件编辑区域点击“添加web部件”,选择“高级web部件库和选项” and import .dwp file
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace AJAXEnabledWebControlRegular
{
public class AjaxEnabledControl : Microsoft.SharePoint.WebPartPages.WebPart
{
private Calendar myCalendar;
private Label lblShowDate;
void SetCalendarStyle(Calendar myCalendar)
{
myCalendar.BackColor = Color.White;
myCalendar.BorderColor = Color.FromName("#999999");
myCalendar.CellPadding = 4;
myCalendar.DayNameFormat = DayNameFormat.Shortest;
myCalendar.Font.Name = "Verdana";
myCalendar.Font.Size = FontUnit.Point(8);
myCalendar.ForeColor = Color.Black;
myCalendar.Height = Unit.Pixel(180);
myCalendar.Width = Unit.Pixel(200);
myCalendar.SelectedDayStyle.BackColor = Color.FromName("#666666");
myCalendar.SelectedDayStyle.Font.Bold = true;
myCalendar.SelectedDayStyle.ForeColor = Color.White;
myCalendar.TodayDayStyle.BackColor = Color.FromName("#CCCCCC");
myCalendar.TodayDayStyle.ForeColor = Color.Black;
myCalendar.SelectorStyle.BackColor = Color.FromName("#CCCCCC");
myCalendar.WeekendDayStyle.BackColor = Color.FromName("#FFFFCC");
myCalendar.OtherMonthDayStyle.ForeColor = Color.FromName("#808080");
myCalendar.NextPrevStyle.VerticalAlign = VerticalAlign.Bottom;
myCalendar.DayHeaderStyle.BackColor = Color.FromName("#CCCCCC");
myCalendar.DayHeaderStyle.Font.Bold = true;
myCalendar.DayHeaderStyle.Font.Size = FontUnit.Point(7);
myCalendar.TitleStyle.BackColor = Color.FromName("#999999");
myCalendar.TitleStyle.BorderColor = Color.Black;
myCalendar.TitleStyle.Font.Bold = true;
}
private void EnsurePanelFix()
{
if (this.Page.Form != null)
{
String fixupScript = @"_spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
function _initFormActionAjax()
{
if (_spEscapedFormAction == document.forms[0].action)
{
document.forms[0]._initialAction = document.forms[0].action;
}
}
var RestoreToOriginalFormActionCore = RestoreToOriginalFormAction;
RestoreToOriginalFormAction = function()
{
if (_spOriginalFormAction != null)
{
RestoreToOriginalFormActionCore();
document.forms[0]._initialAction = document.forms[0].action;
}
}";
ScriptManager.RegisterStartupScript(this, typeof(AjaxEnabledControl), "UpdatePanelFixup", fixupScript, true);
}
}
private void myCalendar_SelectionChanged(object sender, EventArgs args)
{
lblShowDate.Text = myCalendar.SelectedDate.ToString();
}
protected override void CreateChildControls()
{
base.CreateChildControls();
//修改wws3.0脚本更改doPostBack() 函数
//保证不会发生整个页面回发事件
EnsurePanelFix();
UpdatePanel myUpdatePanel = new UpdatePanel();
ScriptManager myScriptManager = new ScriptManager();
myCalendar = new Calendar();
SetCalendarStyle(myCalendar);
lblShowDate = new Label();
//设置控件的属性
myCalendar.ID = "cldDateSelector";
lblShowDate.ID = "lblShowDate";
lblShowDate.Text = string.Empty;
myScriptManager.ID = "scriptHandler";
myUpdatePanel.ID = "refleshScope";
myUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
myUpdatePanel.ChildrenAsTriggers = true;
//添加日历的事件处理
myCalendar.SelectionChanged += new EventHandler(myCalendar_SelectionChanged);
//把日历控件和标签添加到UpdatePanel中
myUpdatePanel.ContentTemplateContainer.Controls.Add(myCalendar);
myUpdatePanel.ContentTemplateContainer.Controls.Add(lblShowDate);
//在页面上注册UpdatePanel和ScriptManager控件
this.Controls.Add(myScriptManager);
this.Controls.Add(myUpdatePanel);
}
}
}
注册该控件为SafeControl,打开SharePoint网站下的web.config(这个网站是您要向其添加web部件的站点),将下面标记添加到web.config 的 <SafeControls> </SafeControls> 部分:
<SafeControl Assembly="(dllname), Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" Namespace=”(namespace of dll)" TypeName="*" Safe="True" />
<?xml version="1.0"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
<Assembly>(dllname)Version=1.0.0.0,Culture=Neutral, PublicKeyToken=null</Assembly>
<TypeName>dllname.classname </TypeName>
<Title>…Part</Title>
</WebPart>
0 Comments:
Post a Comment
<< Home