How can I write a user control package to replace quickpart!
The procedure is as follows:
1. Open vs2008 and create a web part project named superwebpart.
Note: Windows SharePoint Services 3.0 tool: Visual Studio 2008 extensions 1.2, as follows:
Http://www.microsoft.com/downloads/details.aspx? Familyid = 7bf65b28-06e2-4e87-9bad-086e32185e68 & displaylang = ZH-CN (Chinese Version)
Http://www.microsoft.com/downloads/details.aspx? Displaylang = en & familyid = 7bf65b28-06e2-4e87-9bad-086e32185e68 (English version)
Create a project, for example:
2. Click OK. In the project, create a file named treewebpart. CS class library and a file of the Web Part type (named superwebpart), for example:
3. in the treewebpart. write code in the CS class library file. This class inherits system. web. UI. webcontrols. webparts. editorpart, which is used to edit the system. web. UI. webcontrols. webparts. webpart control, and you need to override applychanges, syncchanges, createchildcontrols and other methods,
Complete codeAs follows::
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using Microsoft. SharePoint;
Using system. IO;
Using system. configuration;
Namespace superwebpart
{
Class treewebpart: editorpart
{
Private dropdownlist ddltree = new dropdownlist ();
Public override bool applychanges ()
{
Superwebpart part = (superwebpart) webparttoedit;
Part. url = This. ddltree. selecteditem. value;
Return true;
}
Public override void syncchanges ()
{
}
Protected override void createchildcontrols ()
{
Base. createchildcontrols ();
// Find all files from the wpresources directory
String [] A = system. Io. Directory. getfiles (httpcontext. Current. server. mappath ("../wpresources "));
Fileinfo Info;
For (INT I = 0; I <A. length; I ++)
{
Info = new fileinfo (A [I]);
Ddltree. Items. Add (info. Name );
}
Controls. Add (ddltree );
}
Protected override void renderchildren (system. Web. UI. htmltextwriter writer)
{
Base. renderchildren (writer );
}
Public override void rendercontrol (system. Web. UI. htmltextwriter writer)
{
Base. rendercontrol (writer );
}
}
}
4. The complete code of the superwebpart. CS class library file is as follows, which must be inherited from the webparts and iwebeditable interfaces:
Using system;
Using system. runtime. interopservices;
Using system. Web. UI;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. serialization;
Using system. collections;
Using system. componentmodel;
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. webcontrols;
Using Microsoft. Sharepoint. webpartpages;
Namespace superwebpart
{
[GUID ("d833c329-1066-4c59-a73b-38341ae07797")]
Public class superwebpart: system. Web. UI. webcontrols. webparts. webpart, iwebeditable
{
Public superwebpart ()
{
}
Private usercontrol;
Protected override void createchildcontrols ()
{
Base. createchildcontrols ();
Controls. Clear ();
If (this. url! = String. Empty)
{
Usercontrol = (usercontrol) page. loadcontrol (@ "/wpresources/" + this. url );
Controls. Add (usercontrol );
}
}
Protected override void render (htmltextwriter writer)
{
Ensurechildcontrols ();
If (usercontrol! = NULL)
{
Usercontrol. rendercontrol (writer );
}
}
Private string _ url = string. empty;
[Personalizable]
[Webbrowsable]
[Webdisplayname ("enter a control file manually")]
[Webdescription ("only the file name is required")]
[Category ("personalized settings")]
Public String URL
{
Get {return _ URL ;}
Set {_ url = value ;}
}
// Add the treewebpart to the Russian editorparts
Public editorpartcollection createeditorparts ()
{
Treewebpart OBJ = new treewebpart ();
OBJ. ID = This. ID + "_ selecttreewebpart ";
OBJ. Title = "Select User Control by zhangyun ";
Arraylist editorparts = new arraylist ();
Editorparts. Add (OBJ );
Return new editorpartcollection (editorparts );
}
}
}
5. After compilation is successful,
(1) Put the generated superwebpart. dll in the bin directory corresponding to the SharePoint website c: \ Inetpub \ wwwroot \ WSS \ virtualdirectories \ 8080 \ bin
Add the following code to the <safecontrol> </safecontrol> code in the webconfig file of the SharePoint Website:
<Safecontrol Assembly = "superwebpart, version = 1.0.0.0, culture = neutral, publickeytoken = 9f4da001_c38ec5"
Namespace = "superwebpart" typename = "*" Safe = "true"/> and modify <trust level = "full" originurl = ""/>
(2). Write the user control (?. Ascx) files are stored in the wpresources directory corresponding to the SharePoint website. c: \ Inetpub \ wwwroot \ WSS \ virtualdirectories \ 8080 \ wpresources
In this way, the superwebpart user control package is developed, and the effect is the same as that of quickpart. The final effect is as follows: