1. 判断模式:设计模式
Design Model
protected override void Render(HtmlTextWriter writer)
{
bool isDesign = this.WebPartManager != null && this.WebPartManager.DisplayMode != WebPartManager.BrowseDisplayMode;
if (isDesign)
{
writer.Write("<div style='color:red'>the current web part is in design module.</div>");
}
else
{
base.Render(writer);
}
}
2. 注册JS和CSS文件应在哪个方法中?
SharePoint的WebPart开发中,有时候我们需要调用外部的JS文件和CSS样式文件。而在ASP.NET的Web开发中,直接在<head>标签中写就可以了。其实在WebPart开发中,只要运态的加在网页的<Head>标签中就可以了。但在WebPart的生命周期中,各个方法是有执行顺序的。因此加载JS文件和CSS样式文件也应该写在正确的方法中。
在WebPart开发中,有一个事件:OnPreRender,这个事件是呈现控件之前最后一个可以修改控件属性,因此,注册JS文件,CSS样式文件可以在此事件中加入。C#代码如下:
Register CSS and JS File
protected void RegisterCommonJs(string jsFileName)
{
//MCSCommonJs.Add( jsFileName );
string jsPath = base.ResolveUrl(Constant.RESOURCES_PATH) + jsFileName ;
Page.ClientScript.RegisterClientScriptInclude(typeof(BaseSPWebPart), jsFileName.ToLower() , jsPath);
}
protected void RegisterCommonCss( string fileName )
{
string id = fileName.ToLower().Replace(".","_");
foreach( Control ctl in this.Page.Header.Controls )
{
if( ctl.ID == id )
return ;
}
HtmlLink link1 = new HtmlLink();
link1.ID = id;
link1.Attributes["type"] = "text/css";
link1.Attributes["rel"] = "stylesheet";
link1.Attributes["href"] = base.ResolveUrl(Constant.RESOURCES_PATH) + fileName;
this.Page.Header.Controls.Add(link1);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
this.RegisterCommonJs("MCSCore.js");
this.RegisterCommonCss("Core.css");
}
public static class Constant
{
public const string PAGE_PATH = "~/_layouts/code_art/";
public const string MOSS_CONTROLTEMPLATES_PATH = "~/_controltemplates/";
public const string RESOURCES_PATH = "~/_layouts/code_art/";
}
2. 操作ViewState,一般在OnLoad事件或之后的事件中。
3. 输出CSS 和 Javascript可以在Rendering或RenderContent方法中。
4. 直接输出Html,需要在Render或RenderContent方法中。
5.错误处理,需要在RenderContent方法中
ErrorDeal
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
//统一的错误处理
RenderErrors(writer);
}
/// <summary>
/// /// 呈现所有错误信息
/// /// </summary>
/// /// <param name="w"></param>
private void RenderErrors(HtmlTextWriter w)
{
foreach (Exception ex in _Exceptions)
{
RenderError(ex, w);
}
//清空错误信息
_Exceptions.Clear();
}
protected void RenderError(Exception ex, HtmlTextWriter w)
{
if (_EnableShowErrorDetail)
{
w.Write(ex.Message);
w.Write("<br/>");
w.Write(ex.StackTrace);
w.Write("<br/>");
if (ex.InnerException != null)
RenderError(ex.InnerException, w);
}
else
{
w.Write("发生错误:" + ex.Message);
}
}
6. 在后台调用前台的方法
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
RenderScriptForSPList();
}
private void RenderScriptForSPList()
{
StringBuilder ss = new StringBuilder();
ss.Append("<script type=\"text/javascript\">");
ss.Append(" $(function () {");
ss.AppendFormat("GetListData('#{0}','#{1}','#{2}','#{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}');", TabUlID,
LiSelectedID, ContentID, ImageID, ListName, TabSelectedName,
JobInfoNumber, ItemLinkAddress, ImageLinkAddress, InfoClassType.GetHashCode(),LanNum);
ss.Append(" });");
ss.Append("</script>");
Page.ClientScript.RegisterStartupScript(Page.GetType(), "m" + Guid.NewGuid().ToString().Replace("-", ""), ss.ToString());
}
7. WebPart属性
[Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
WebDisplayName("WebPart类型"),
WebDescription("选择Display为显示个人信息,选择Edit为编辑个人信息")]