1. 程式人生 > >步步為營 SharePoint 開發學習筆記系列 五、Web Part開發

步步為營 SharePoint 開發學習筆記系列 五、Web Part開發

概要

    現在有兩種不同的Web部件。老的WSS風格的WebPart依賴於Microsoft.SharePoint.dll,必須繼承自WSS 2.0所定義的WebPart基類,其名稱空間為Microsoft.SharePoint.WebPartPages。新的ASP風格WebPart依賴於System.Web.dll,必須繼承自不同的一個由ASP.NET 2.0定義的WebPart基類,其名稱空間為System.Web.UI.WebControls.WebParts。

我們將從簡單的hello Word web part 開始:

程式碼設計:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization; using System.Web.UI; namespace LearnWriteWebPart.Webpart { [ToolboxData("<{0}:SampleWebPart runat=server></{0}:SampleWebPart>"
)] [XmlRoot(Namespace = "LearnWriteWebPart.Webpart")] public class SampleWebPart : WebPart { private string _Text = "Hello World!"; [WebBrowsable(true), Personalizable(true)] public string Text { get { return _Text; } set { _Text = value; } } protected override void Render(System.Web.UI.HtmlTextWriter writer)
{ writer.Write(_Text); } } }

在我們建好的spring blog中作如下操作把webpart加入站點中:

1、首先把自己寫的webpart啟用。如下點選populate gallery.

image

2、在spring blog載入web part,首先點選edit page.

image

3、再點選add web a part 後,選擇我們的samplewebpart.

image

4、結果如我們所想的一樣

image

接著我們做一個複雜點的,使用者登陸web part.要做這樣一個web part,我們先要加一個 user control,名字命名為LoginUserControl.ascx,

程式碼設計如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="LoginUserControl.ascx.cs" Inherits="WebUserControl_LoginUserControl" %> <style type="text/css"> .style1 { width: 32%; height: 28px; } .style2 { width: 128px; } </style> <table class="style1"> <tr> <td class="style2"> <asp:Label ID="lblUserAccount" runat="server" Text="UserAccount:"></asp:Label> </td> <td> <asp:TextBox ID="txtUserAccount" runat="server" TabIndex = "1"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvUserAccount" runat="server" ControlToValidate="txtUserAccount" ErrorMessage="使用者名稱不能為空"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style2"> <asp:Label ID="lblPassword" runat="server" Text="Password:"></asp:Label> </td> <td> <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" TabIndex="2"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassWord" ErrorMessage="密碼不能為空"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style2"> &nbsp;</td> <td> <asp:Button ID="btnLogin" runat="server" TabIndex="3" onclick="btnLogin_Click" Text="Login" /> <asp:Label ID="lblResult" runat="server" BorderColor="Red" ForeColor="Red"></asp:Label> </td> </tr> </table>

後臺的程式碼如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using LearnWriteWebPart.BE; using LearnWriteWebPart.BO; using LearnWriteWebPart.Util; public partial class WebUserControl_LoginUserControl : System.Web.UI.UserControl { /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SetInitial(); } } protected void btnLogin_Click(object sender, EventArgs e) { if (!CheckUserAccountLength()) return; if (!CheckPasswordLength()) return; this.CheckLogin(); } #region Private Method /// <summary> /// Initial Control /// </summary> private void SetInitial() { txtUserAccount.Focus(); CodeHelper.DisableIMEModes(new TextBox[] { txtUserAccount, txtPassword}); } /// <summary> /// Check login /// </summary> private void CheckLogin() { UserBE userBE = new UserBE(); UserBO userBO = new UserBO(); userBE.UserAccount = txtUserAccount.Text.Trim(); userBE.Password = txtPassword.Text.Trim(); if (userBO.CheckUserLogin(userBE)) { lblResult.Text = Constants.SUSSCESSFULLOGIN_USER; } else { lblResult.Text = Constants.ERRORLOGIN_USER; } } /// <summary> /// check password length /// </summary> /// <returns></returns> private bool CheckPasswordLength() { int length = txtPassword.Text.Trim().Length; if (length < 6) { lblResult.Text = Constants.PASSWORDMINLENGTH_USER; return false; } else if (length > 20) { lblResult.Text = Constants.PASSWORDMAXLENGTH_USER; return false; } return true; } /// <summary> /// Check user account length /// </summary> /// <returns></returns> private bool CheckUserAccountLength() { int length = txtUserAccount.Text.Trim().Length; if (length > 20) { lblResult.Text = Constants.USERACCOUNTMAXLENGTH_USER; return false; } return true; } #endregion }

UserBE和UserBO的程式碼相信大家都懂的,我就不貼出來了.

WebPart的程式碼如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization; using System.Web.UI; using LearnWriteWebPart.Util; namespace LearnWriteWebPart.Webpart { [ToolboxData("<{0}:UserWebPart runat=server></{0}:UserWebPart>")] [XmlRoot(Namespace = "LearnWriteWebPart.Webpart")] public class UserWebPart : WebPart { #region [Private Variable] private string _ListName = string.Empty; private string _Url = string.Empty; private string TheListName = Constants.LISTNAME_USER; private UserControl _userControl; #endregion #region [Custom Properties] /// <summary> /// This list naem /// </summary> [WebBrowsable(false), Personalizable(true)] public string ListName { get { return _ListName; } set { _ListName = value; } } /// <summary> /// The list Url /// </summary> [WebBrowsable(false), Personalizable(true)] public string Url { get { return _Url; } set { _Url = value; } } #endregion #region [Constructors] /// <summary> /// The sample constructor /// </summary> public UserWebPart() { this.ListName = TheListName; } #endregion #region [Override Methods] /// <summary> /// Override method to OnInit method /// </summary> /// <param name="e">The EventsArgs object</param> protected override void OnInit(EventArgs e) { base.OnInit(e); SetWebPartTitleAndUrlWhenAdded(this.ListName, this.Url); AddControlToWebPart(); } #endregion #region [Private Methods] /// <summary> /// Add Login Control /// </summary> private void AddControlToWebPart() { Type controlType = Type.GetType("ASP.webusercontrol_loginusercontrol_ascx,Web_deploy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234567f94a516f5"); _userControl = (UserControl)this.Page.LoadControl(controlType, null); this.Controls.Add(_userControl); } /// <summary> /// Set webpart title and url /// </summary> /// <param name="ListName"></param> /// <param name="Url"></param> private void SetWebPartTitleAndUrlWhenAdded(string ListName, string Url) { this.Title = this.ListName; this.TitleUrl = this.Url; } #endregion } }

結果如下圖:

image

?
1

出現我們預想的畫面。

接下來我們再做一個使用者祥細資訊的webpart和editwebpart.