使用ASP.NET幾種Webpart部署方式
使用ASP.NET幾種Webpart部署方式:
1.使用ASP.NET 2.0的Webpart部署方式,聲明SafeContorl之后上載到Sharepoint的Webpart gallery中,MSDN上的Walkthrough: Creating a Basic Web Part 有詳細(xì)介紹.
2.使用SharePoint的Webpart,裝一個VS Extensions for SharePoint之后有一個項(xiàng)目類型叫做webpart,寫一個webpart,然后F5,就會把webpart部署到你的SharePoint的站點(diǎn)中.實(shí)際上也是部署了一個Feature. 其原理MSDN上也有介紹:Walkthrough: Creating a Basic SharePoint Web Part
3.kaneboy在他的blog中提到過使用Feature的部署DelegateContorl的方式部署一個ascx文件到站點(diǎn)集的Feature。
以上三種方式各有優(yōu)缺點(diǎn),前兩種方式采用純粹的Webpart的形式,開發(fā)起來很麻煩,特別是一些豐富的用戶界面會變得很麻煩。第三種方式雖然解決了前兩種方式的不足,但是沒有Code-behind,做企業(yè)級開發(fā)把 code寫在ascx文件中實(shí)在有點(diǎn)不合適,而且使用Feature部署以后管理起來不是很方便。那么今天我就來Demo一種使用Code-behind 用戶控件作為WebPart的方式,而且將部署的UserContorl便于管理。
這里使用一個工具叫SmartPart, 這個工具在WSS 2.0時代就有,現(xiàn)在又針對與WSS 3.0的版本。下載回來一個msi安裝包,不知道為什么我在兩個VPC上安裝都沒有成功,解壓縮到臨時目錄下之后就沒反映了。沒辦法,一些還得自己動手豐衣足食阿。在臨時目錄下有一個Cab文件,把這個文件打開之后研究一下,大概明白了安裝的步驟。
Step(1): 將SmartPart.dll部署到GAC.
Step(2):新建一個Web Project,在里面開發(fā)你的用戶控件,Demo的用戶控件叫UCPart.ascx。
前臺代碼:
- <%@ Control Language="C#" CodeFile="UCPart.ascx.cs" Inherits=
"DemoWebPart.UCPart" CompilationMode="Always" %>- <asp:TextBox ID="txbDate" runat="server">
- </asp:TextBox>
后臺代碼:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- namespace DemoWebPart
- {
- public partial class UCPart : System.Web.UI.UserControl
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- this.txbDate.Text = DateTime.Now.ToShortDateString();
- }
- }
- }
功能很簡單,就是在后臺代碼中為一個textbox賦一個值。編譯一下通過之后,把這個用戶控件連同其后臺代碼文件copy到<%IIS Root Dir%>\wss\VirtualDirectories\xxxx\UserContorls下。
其中<%IIS Root Dir%>為IIS的根目錄,xxxx為當(dāng)前Web Application的端口號。如果你***次做這個操作,是需要手工建立一個UserContorls文件夾的。
- <SafeControl Assembly="SmartPart, Version=1.1.0.0, Culture=neutral,
PublicKeyToken=dd064a5b12b5277a" Namespace="SmartPart" TypeName="*" />
同時更改<%IIS Root Dir%>\wss\VirtualDirectories\xxxx\Web.Config.聲明SaftContorl.以上介紹使用ASP.NET幾種Webpart部署方式。
【編輯推薦】