SharePoint 2003 WebPart Development Notes

xiaoxiao2021-03-06  54

SharePoint 2003 WebPart Development Notes

Chapter 1 Develop a Web Part Project

The first quarter creates a Web Part Library Project project

1. Fill a reference to Microsoft.SharePoint.dll in the project

a) If you are developing on Microsoft.SharePoint Server, you can find it directly in the reference.

b) If not, you have to use the microsoft.sharepoint.dll file COPY to your directory.

2. Fill in the reference of System.xml.

The second quarter is preparing for the development of the development

1. Specify Build Output Path.

a) Specify the directory where Assembly is located. Such as: c: / inetpub / wwwroot / bin.

b) I want to post into the GAC (C: / Windows / askEMBLY), can only register with the copy and .net command Gacutil.exe.

c) Publish into other directories (for example, / inetpub / wwwroot / mydir or mydir2 / bin), which you need to modify the web.config file, add the following node in

2. Set the version number

a) Open file assemblyinfo.cs, modify

[assmbly: assemblyversion "

1.0.0

.0 ")]

3. Set a strong name for assembly accessories

WebPART is developed on the Internet or intranet. In order to create a security reason when you create WebPart, you must use a homogeneous webpart so that users can accurately hit the WebPart you provide.

a) Generate a strong name file. Sn.exe -k c: /keypair.snk

b) Fill in the name of the name

[Assembly: AssemblyKeyFile ("c: //keypair.snk")]]]]

Section 3 Code Development

Assume that you are developed using WebPart Library.

(1) Defining Toolbox Data

Set the attribute value of the class. Add the property settings for this class before the class declaration.

[ToolBoxData ("<{0}: WebPart1 Runat = Server> ")] // Note that the definition of Target is the same as the class name

Public Class WebPart1: Microsoft.SharePoint.WebPartPages.WebPart {

...

}

(2) Defining XML Namespace

If you want to successfully import user-defined WebPart, you must set the XML NameSpace property for each WebPart. This action is done when the Web Part Class is defined.

[XMLROOT (Namespace = "MyWebParts")]

(3) logic implementation

/ / -------------------------------------------------------------------------------------------- --------------------

// file: SimpleWebPart.cs

//

// Purpose: a Sample Web Part That Demonstrates How To Create a Basic // Web Part.

/ / -------------------------------------------------------------------------------------------- --------------------

Using system;

Using system.componentmodel;

Using system.Runtime.InteropServices;

Using system.Web.ui;

Using system.Web.ui.webcontrols;

Using system.xml.serialization;

USING Microsoft.SharePoint;

Using Microsoft.SharePoint.WebPartPages;

Using microsoft.sharepoint.utilities;

Using system.Web.ui.htmlcontrols;

Namespace mywebparts

{

///

/// this Web Part Changes It's OWN Title and Implements A Custom Property.

///

[XMLROOT (Namespace = "MyWebParts")]

Public Class SimpleWebPart: WebPart

{

Private const string defaultText = "Hello";

Private string text = defaultText;

// Declare Variables for HTMLControls User Interface Elements.

Htmlbutton_mybutton;

HTMLINPUTTEXT_MYTEXTBOX;

// Event Handler for _myButton Control That Sets

// Title Property to The Value in _MytextBox Control.

Public void_mybutton_click (Object Sender, Eventargs E)

{

THIS.TITLE = _MytextBox.Value;

Try

{

this.SaveProperties = true;

}

Catch

{

Caption = "Error ... Could Not Save Property."

}

}

// Override the asp.net web.ui.controls.createchildControls

//Method to create the objects for the Web Part's Controls.

Protected Override Void CreateChildControls ()

{

// CREATE _MYTEXTBOX CONTROL.

_mytextbox = new htmlinputText ();

_mytextbox.value = "";

Controls.add (_mytextbox);

// Create_Mybutton Control and Wire Its Event Handler.

_mybutton = new htmlbutton ();

_MyButton.innertext = "set web part title";

_MyButton.serverClick = New EventHandler (_MYBUTTON_CLICK); controls.add (_mybutton);

}

[Browsable (True), Category ("Miscellaneous"),

DEFAULTVALUE (DefaultText),

WebPartStorage (Storage.Personal),

FriendlyName ("Text"), Description ("Text Property)]]]]

Public String Text

{

get

{

Return TEXT;

}

set

{

TEXT = VALUE;

}

}

Protected Override Void RenderWebPart (HTMLTextWriter Output)

{

RenderChildren (Output);

// Securely Write out HTML

Output.write ("
TEXT Property:" Spencode.htmlencode (Text));

}

}

}

Note: By default, the confirmation level is set to WSS_MINIMAL, which cannot access SharePoint Object Model. In order to set the SaveProperties property, you must perform the following three actions.

a) CREATE A CUSTOM Policy File for Your Assembly.

b) Install your assembly in the global assembly cache.

c) Increase The Trust Level for the entire Virtual Server.

Section IV Configuring your Web Part

(1) Register your web part as a SafeControl

As a security approach, Windows SharePoint Services requires you to register Web Part's assembly and Namespace as a SafeControl file in the web.config file.

Next, a WebPart assembly is registered as a SafeControl, editing the c: /inetpub/wwroot/web.config file, and fill the following nodes into the block. Format Description:

SafeControl Attributes:

Assembly = "[assembly" - The .NET Assembly In Which The Control IS Conta Contain Version, Culture, _U97? Nd Public Key Token Information.

Namespace = "[namespace]" - The .NET Namespace in Which the control is defined.

Typename = "[typeename]" - The .NET Class Name of the control. You can type an asseterisk (*) Wildcard Character To Indicate All Typenames in a namespace.

SAFE = "[true | false]" - Specifies WHETHER A Web Part Or Web Form Control Is Safe and Can Be Displayed ON A Web Parts Page. This attribute is true

AskEMBLY = "SimpleWebPart, Version =

1.0.0

.0, culture = neutral, publickeytoken = DEF

148956C

61A

16B "

Namespace = "MyWebParts"

Typename = "*"

Safe = "true"

/>

Note: Replace your Web Part's assembly. Replace the value of publickeyToken, to get this value correctly, use the sn.exe command.

Sn.exe -t c: /inetpub/wwwroot/bin/simplewebpart.dll

In addition to this approach, there is another kind of to copy your assembly to the GAC, view its properties, or get it.

Section 5 Create a Web Part Definition file (.dwp)

A very simple DWP file completes the property settings for a separate WebPart. With DWP import, you can realize the web part in SharePoint.

In a web part definition file, Assembly and TypeName are must be set.

The content is as follows

AssemblyName (with no .dll extension), Version = VersionNumber, Culture = Culture, PublickeyToken = PublickeyToken

WebPartNamespace.WebPartClassName

defaultwebparttitle </ title></p> <p><Description> WebPartDescription </ description></p> <p></ WebPart></p> <p>Section 6 Import your web part to Web Part Page</p> <p>1. Open a Web Part Page on your server.</p> <p>.</p> <p>Click Import.</p> <p>3. Browse to the location of the SimpleWebPart.dwp file and click the Upload button. After uploading, the page will refresh and "My Simple Web Part" should be displayed under Imported Web Part.4. Drag the icon next to "My Simple Web Part "to a zone on the web part page.</p> <p>5. Type Some Text In The Text Box, And The Click SET Web Part Title To Test The Part.</p> <p>Chapter II Create a custom user attribute of a web part</p> <p>WebPart itself has built a part of the basic properties, such as the height, title, etc. of WebPart. But when you need more complex settings for WebPart, your own properties can no longer meet your requirements, then you must use custom properties.</p> <p>The first section creates a custom user attribute</p> <p>(1) Modify XML Settings</p> <p>In addition to some special requirements, you can define custom controls like the Web UserControl of ASP.NET. Since the properties of the Web Part are defined by the user, save in the SharePoint Storage System in XML, you have to make the following to save the custom properties.</p> <p>1. Add a reference to System.xml.dll.</p> <p>2. Define the XML NameSpace property while you define the properties. If you add the XML namespace at the root level, you are not required to assign XML namespaces at the property level. An XML namespace assigned at the property level will override the XML namespace assigned at the root level. The XmlRoot attribute has the following form :</p> <p>[Xmlroot (namespace = "name of namespace")]]]]</p> <p>(2) Creating a property in accordance with the property</p> <p>In each web part custom property, you need to set up the method, and a typical property setting is as follows.</p> <p>[AttributeDeclaration1, AttributeDeclaration2, ...]</p> <p>Public propertyType PropertyVariable</p> <p>Private propertyType PrivatePropertyvariable PRIVATYVARIABLE</p> <p>{</p> <p>get</p> <p>{</p> <p>Return PrivatePropertyVariable;</p> <p>}</p> <p>set</p> <p>{</p> <p>PrivatePropertyVariable = value;</p> <p>}</p> <p>These you define the properties will be able to determine that it is stored and personalized when the user uses your WebPart.</p> <p>(3) Setting the properties of custom properties</p> <p>These properties you set will display the value you set and store in the WebPart property box in the WebPart process. Most of the properties are classes provided in System.comPonentModel Namespace, which you can reference and define them during development. Some properties, such as the WebPartStorage property, which is used to set the properties of WebPart. The property and definition are listed below:</p> <p>Attribute</p> <p>Purpose</p> <p>Browsable Set to false if you do not want to display the custom property in the property pane. Also, if you set the WebPartStorage attribute to Storage.None, your property will not display in the property pane. Category The title of the section of the property pane that you want created to display the custom property. If you do not specify the Category attribute or if you specify the Category attribute as "Default", your custom property is displayed in the Miscellaneous section of the property pane. Note If you specify one of the default categories, such as Appearance, your Category attribute setting is ignored, and your property is displayed in the Miscellaneous section. DefaultValue The default value of the custom property. Specifying the default value minimizes the Web Part's storage requirements by Storing The Property's Value ONLY IT IS DIFFERENT from The Default. Description The Contents of The Tool Tip That Appears When You Pause The Mouse Pointer Over The Custom Property in T E Property Pane. FriendlyNameAttribute The Caption Displayed for The Custom Property In The Property Pane. if you do '</p> <p>t specify this attribute, the actual property name will be displayed in the property pane. ReadOnly Set to true if you want the custom property to be read-only in the property pane. WebPartStorage Set to Storage.Shared to display the custom property in the property pane when the user is in shared view of the page. Set to Storage.Personal to display the custom property in the property pane when the user is in Shared or Personal view of the page. Set to Storage.None if you do not want the setting to persist for the custom property. the custom property will not be displayed in the property pane. HtmlDesignerAttribute Used to associate a property builder. (iv) a simple Web Part custom properties with the property that contains examples of user</p> <p>The following example will demonstrate how to define the following custom properties: String, Bool, Int, Float, ENUM, System.Datetime, and system.drawing.knowncolor.</p> <p>/ / -------------------------------------------------------------------------------------------- --------------------</p> <p>// file: WebPartCustomProperties.cs</p> <p>//</p> <p>// Purpose: a Sample Web Part That Implements Custom Properties</p> <p>// of the following type: string, bool, int, float, enum,</p> <p>// system.datetime, and system.drawing.knowncolor</p> <p>//</p> <p>// after building and installing this Web Part, Display</p> <p>// the property pane to see how the user interface</p> <p>// for setting their value is rendered.</p> <p>/ / -------------------------------------------------------------------------------------------- ---------------------</p> <p>Using system;</p> <p>Using system.componentmodel;</p> <p>Using system.Runtime.InteropServices;</p> <p>Using system.Web.ui;</p> <p>Using system.Web.ui.webcontrols;</p> <p>Using system.xml.serialization;</p> <p>USING Microsoft.SharePoint;</p> <p>Using Microsoft.SharePoint.WebPartPages;</p> <p>Namespace WebPartLibrary2</p> <p>{</p> <p>/// <summary></p> <p>/// Summary Description for CustompropertyWebPart.</p> <p>/// </ summary> [DefaultProperty ("text"),</p> <p>ToolboxData ("<{0}: CustomPropertyWebPart Runat = Server> </ {0}: CustomPropertyWebPart>"),</p> <p>XMLROOT (Namespace = "WebPartLibrary2")]]</p> <p>Public Class CustomPropertyWebpart: Microsoft.SharePoint.WebPartPages.WebPart</p> <p>{</p> <p>Const string c_mystringdefault = "sample string";</p> <p>Const Bool C_myBoolDefault = FALSE;</p> <p>Const int c_myintdefault = 20;</p> <p>Const float c_myfloatdefault =</p> <p>33.33F</p> <p>;</p> <p>Public Enum myfarmenum</p> <p>{</p> <p>Barn = 0,</p> <p>Tractor,</p> <p>Hay,</p> <p>Pitchfork</p> <p>}</p> <p>protected myfarmenum_myenum;</p> <p>// Private variables</p> <p>Private string_mystring;</p> <p>Private bool_mybool;</p> <p>Private Int_myint;</p> <p>Private float _myfloat;</p> <p>Private system.datetime_mydatetime;</p> <p>Private system.drawing.knowncolor _mycolor =</p> <p>System.drawing.knowncolor.Red;</p> <p>// conncture</p> <p>Public CustomPropertyWebPart ()</p> <p>{</p> <p>// Initialize private variables.</p> <p>_MYSTRING = C_MYSTRINGDEFAULT;</p> <p>_mybool = c_mybooldefault;</p> <p>_myint = c_myintdefault;</p> <p>_myfloat = c_myfloatdefault;</p> <p>_myenum = myfarmenum.hay;</p> <p>_mydatetime = system.datetime.now;</p> <p>}</p> <p>// Creates a Custom Property That Is A String.</p> <p>// this Property Will Be Displayed As a Text Box in The</p> <p>// Property Pane.</p> <p>// Create a Custom Category in The Property Sheet.</p> <p>[Category ("Custom Properties"]]]]]]</p> <p>// Assign The Default value.</p> <p>[DefaultValue (c_mystringdefault)]</p> <p>// Property Is Available in Both Personalization</p> <p>// and customization mode.</p> <p>[WebPartStorage (Storage.Personal)]</p> <p>// The Caption That Appears in the property sheet.</p> <p>[FriendlyNameAttribute ("Custom String")]]]</p> <p>// The Tool Tip That Appears When Pausing The Mouse Pointer Over</p> <p>// The friendly name in the property Pane.</p> <p>[Description ("Type A String Value.")] // Display The Property In The Property Pane.</p> <p>[Browsable (TRUE)]</p> <p>[XMLELEMENT (ElementName = "MyString")]]]</p> <p>// The Accessor for this property.</p> <p>Public String MyString</p> <p>{</p> <p>get</p> <p>{</p> <p>Return_Mystring;</p> <p>}</p> <p>set</p> <p>{</p> <p>_MYSTRING = Value;</p> <p>}</p> <p>}</p> <p>// Creates a Custom Property That Is A Boolean Value.</p> <p>// this Property Will Display As a Check Box in The</p> <p>// Property Pane.</p> <p>[Category ("Custom Properties"]]]]]]</p> <p>[DefaultValue (c_mybooldefault)]</p> <p>[WebPartStorage (Storage.Personal)]</p> <p>[FriendlyNameAttribute ("Custom Boolean")]]]</p> <p>"" Select to set value to true. ")]]]]]</p> <p>[Browsable (TRUE)]</p> <p>[XMLELEMENT (ElementName = "MyBoolean")]]]]</p> <p>// The Accessor for this property.</p> <p>Public Bool MyBool</p> <p>{</p> <p>get</p> <p>{</p> <p>Return_mybool;</p> <p>}</p> <p>set</p> <p>{</p> <p>_mybool = value;</p> <p>}</p> <p>}</p> <p>// Creates a Custom Property That is an integer.</p> <p>// this Property Will Display As a Text Box in The</p> <p>// Property Pane.</p> <p>[Category ("Custom Properties"]]]]]]</p> <p>[DefaultValue (c_myintdefault)]</p> <p>[WebPartStorage (Storage.Personal)]</p> <p>[FriendlyNameAttribute ("Custom Integer")]]]]</p> <p>"" "Type An Integer Value.")]]]</p> <p>[Browsable (TRUE)]</p> <p>[XMLELEMENT (ElementName = "Myint")]]]]]</p> <p>Public int Myint</p> <p>{</p> <p>get</p> <p>{</p> <p>Return_myint;</p> <p>}</p> <p>set</p> <p>{</p> <p>_myint = value;</p> <p>}</p> <p>}</p> <p>// Creates a Custom Property That Is A Float.</p> <p>// this Property Will Display As a Text Box in The</p> <p>// Property Pane.</p> <p>[Category ("Custom Properties"]]]]]]</p> <p>[DefaultValue (c_myfloatdefault)]</p> <p>[WebPartStorage (Storage.Personal)]</p> <p>[FriendlyNameAttribute ("Custom Float")]]</p> <p>"" "Type A Floating Point Value.")]]]]</p> <p>[Browsable (TRUE)]</p> <p>[XMLELEMENT (ElementName = "Myfloat")]]]]</p> <p>Public float myfloat</p> <p>{</p> <p>get</p> <p>{</p> <p>Return_myfloat;</p> <p>set</p> <p>{</p> <p>_MYFLOAT = Value;</p> <p>}</p> <p>}</p> <p>// Creates A Custom Property That Is A System.dateTime.</p> <p>// this Property Will Display As a Text Box in The</p> <p>// Property Pane.</p> <p>[Category ("Custom Properties"]]]]]]</p> <p>[WebPartStorage (Storage.Personal)]</p> <p>[FriendlyNameAttribute ("Custom Date Time")]]]</p> <p>"" Type a datetime value. ")]]]]</p> <p>[Browsable (TRUE)]</p> <p>[Xmlelement (typeof (system.datetime)]]]</p> <p>Public system.datetime mydatetime</p> <p>{</p> <p>get</p> <p>{</p> <p>Return_mydatetime;</p> <p>}</p> <p>set</p> <p>{</p> <p>_mydatetime = value;</p> <p>}</p> <p>}</p> <p>Public Bool ShouldSerializationMyDateTime ()</p> <p>{</p> <p>Return True;</p> <p>}</p> <p>// Creates a Custom Property That IS An Enum.</p> <p>// this Property Will Be Displayed As a Drop-Down List in The</p> <p>// Property Pane.</p> <p>[Category ("Custom Properties"]]]]]]</p> <p>[DefaultValue (MyFARMenum.hay)]</p> <p>[WebPartStorage (Storage.Personal)]</p> <p>[FriendlyName ("Custom Enum")]]]]]</p> <p>[Description ("Select a value from the dropdown list.")]]</p> <p>[Browsable (TRUE)]</p> <p>Public myfarmenum myenum</p> <p>{</p> <p>get</p> <p>{</p> <p>Return_MYENUM;</p> <p>}</p> <p>set</p> <p>{</p> <p>_MYENUM = VALUE;</p> <p>}</p> <p>}</p> <p>// Creates a Property That Is A Known System Color.</p> <p>// this Property Will Be Displayed As a Drop-Down List in The</p> <p>// Property Pane.</p> <p>[Category ("Custom Properties"]]]]]]</p> <p>[WebPartStorage (Storage.Personal)]</p> <p>[FriendlyNameAttribute ("Custom Color")]]]</p> <p>[Description ("SELECT A Color from The Dropdown List.")]]</p> <p>[Browsable (TRUE)]</p> <p>[Xmlelement (typeof (system.drawing.knowncolor)]]]</p> <p>Public system.drawing.knowncolor mycolor</p> <p>{</p> <p>get</p> <p>{</p> <p>Return_mycolor;</p> <p>}</p> <p>set</p> <p>{</p> <p>_MYCOLOR = VALUE;</p> <p>}</p> <p>}</p> <p>Public Bool ShouldSerializationMycolor ()</p> <p>{</p> <p>Return True;</p> <p>}</p> <p>/// <summary></p> <p>/// Render this Web Part to the Output Parameter Specifie.</p> <p>/// </ summary></p> <p>/// <param name = "output"> /// The HTML Writer to Write Out To</p> <p>/// </ param></p> <p>Protected Override Void RenderWebPart (HTMLTextWriter Output)</p> <p>{</p> <p>// Write Stored Property Values ​​to the Web Part.</p> <p>Output.write ("<B> Stored Property VALUES </ B>");</p> <p>Output.write ("<br> <b> String: </ b>" </p> <p>this.mystring;</p> <p>Output.write ("<br> <b> Boolean: </ b>" </p> <p>THIS.myBool.toString ());</p> <p>Output.write ("<br> <B> INT: </ B>" </p> <p>this.myint.toString ());</p> <p>Output.write ("<br> <b> float: </ b>" </p> <p>this.myfloat.tostring ());</p> <p>Output.write ("<br> <B> DateTime: </ b>" </p> <p>this.mydatetime.tostring ());</p> <p>Output.write ("<br> <b> ENUM: </ B>" </p> <p>THIS.MYENUM.TOSTRING ());</p> <p>Output.write ("<br> <B> Color Enum: </ b>" </p> <p>this.mycolor.tostring ());</p> <p>}</p> <p>}</p> <p>}</p> <p>(5) Post your procedure</p> <p>slightly</p> <p>(6) The effect of the defined attribute is displayed in WebPart</p> <p>Property Type</p> <p>Displayed in Property Pane as</p> <p>Bool Check Box DateTime Text Box Enum Dropdown Int Text Box String Text Box</p> <p>Chapter III Create a WebPart with TOOL PART</p> <p>The first quarter adds a GetToolParts method in your WebPart class</p> <p>To determine which tool parts to include in the property pane for your Web Part, you must override the GetToolParts method of the WebPart base class in your Web Part's class. The GetToolPart method returns an array of references to the new ToolPart objects that will be displayed in the property pane for your Web Part. The ToolPart objects are rendered by the tool pane in the order listed in the array. This provides you with the ability to show multiple ToolParts if your Web Part requires them. Note that when you override the WebPart class's GetToolParts method, the default ToolPart objects (WebPartToolPart and CustomPropertyToolPart) are not displayed automatically. You must include code to display them.By default, the GetToolParts method will return the CustomPropertyToolPart and WebPartToolPart objects. The CustomPropertyToolPart object will display the built-in Tool Part That Is Shown by Default for Custom Properties As Described in The "Creating A Web Part with Custom Properties" PR ogramming task. If you want to display some of the custom properties for your Web Part using the built-in custom properties tool part, you must include a reference to the CustomPropertyToolPart object in the array of references returned by your GetToolParts method. The WebPartToolPart object .</p> <p>. ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,</p> <p>/// <summary></p> <p>/// Gets The Custom Tool Parts for this Web Part by overriding the</p> <p>/// GetToolparts Method of the WebPart Base Class. You Must Implement / // Custom Tool Parts In a Separate Class That Derives from</p> <p>/// Microsoft.SharePoint.WebPartPages.Toolpart.</p> <p>/// </ summary></p> <p>/// <returns> an Array of References to Toolpart Objects. </ returns></p> <p>// public override toolpart [] gettoolparts ()</p> <p>// {</p> <p>// Toolpart [] Toolparts = New Toolpart [2];</p> <p>// WebPartToolPart WPTP = New WebPartToolPart ();</p> <p>// CustomPropertyToolPart Custom = New CustomPropertyToolPart ();</p> <p>// Toolparts [0] = Custom;</p> <p>// Toolparts [1] = WPTP;</p> <p>// Return Toolparts;</p> <p>//}</p> <p>The Following Code Sample Shows A Complete Web Part Class That Wepele Custom Property That Will Be Displayed In a Custom Tool Part.</p> <p>/ / -------------------------------------------------------------------------------------------- ---------</p> <p>// file: SimpleWebPart.cs</p> <p>//</p> <p>// Purpose: a Simple Web Part That Implements One Custom</p> <p>// Property.</p> <p>/ / -------------------------------------------------------------------------------------------- ---------</p> <p>Using system;</p> <p>Using system.componentmodel;</p> <p>Using system.Runtime.InteropServices;</p> <p>Using system.Web.ui;</p> <p>Using system.Web.ui.webcontrols;</p> <p>Using system.xml.serialization;</p> <p>USING Microsoft.SharePoint;</p> <p>Using Microsoft.SharePoint.WebPartPages;</p> <p>Namespace WebPartLibrary2</p> <p>{</p> <p>/// <summary></p> <p>/// Summary Description for SimpleWebPart.</p> <p>/// </ summary></p> <p>[DefaultProperty ("Text"),</p> <p>ToolboxData ("<{0}: SimpleWebPart Runat = Server> </ {0}: SimpleWebPart>"),</p> <p>XMLROOT (Namespace = "WebPartLibrary2")]]</p> <p>Public Class SimpleWebPart: Microsoft.SharePoint.WebPartPages.Webpart</p> <p>{</p> <p>Private const string defaultText = "Your text";</p> <p>Private string text = defaultText;</p> <p>[Browsable (False), Category ("Miscellaneous"), DEFAULTVALUE (DefaultText),</p> <p>WebPartStorage (Storage.Personal),</p> <p>FriendlyName ("Text"), Description ("Text Property)]]]]</p> <p>Public String Text</p> <p>{</p> <p>get</p> <p>{</p> <p>Return TEXT;</p> <p>}</p> <p>set</p> <p>{</p> <p>TEXT = VALUE;</p> <p>}</p> <p>}</p> <p>/// <summary></p> <p>/// Gets The Custom Tool Parts for this Web Part by</p> <p>/// Overriding the gettoolparts method of the webpart</p> <p>/// base class. You Must Implement Custom Tool Parts in</p> <p>/// a Separate Class That Derives from</p> <p>/// Microsoft.SharePoint.WebPartPages.Toolpart.</p> <p>/// </ summary></p> <p>/// <returns></p> <p>/// ARRAY OF REFERENCES to Toolpart Objects.</p> <p>/// </ returns></p> <p>Public Override Toolpart [] gettoolparts ()</p> <p>{</p> <p>Toolpart [] Toolparts = New Toolpart [3];</p> <p>WebPartToolPart WPTP = New WebPartToolPart ();</p> <p>CustomPropertyToolPart Custom = New CustomPropertyToolPart ();</p> <p>Toolparts [0] = Custom;</p> <p>Toolparts [1] = WPTP;</p> <p>// this is the custom Toolpart.</p> <p>Toolparts [2] = new webpartlibrary2.toolpart1 ();</p> <p>Return Toolparts;</p> <p>}</p> <p>/// <summary></p> <p>/// Render this Web Part to the Output Parameter Specifie.</p> <p>/// </ summary></p> <p>/// <param name = "Output"></p> <p>/// The HTML Writer to Write Out To</p> <p>/// </ param></p> <p>Protected Override Void RenderWebPart (HTMLTextWriter Output)</p> <p>{</p> <p>Output.write (Spencode.htmlencode ("Your Custom Text IS: <B> THISTEXT " </ b> <hr> "));</p> <p>}</p> <p>}</p> <p>}</p> <p>Section 2 Create Custom Toolpart's Class</p> <p>(1) Join a new class definition in your project</p> <p>First, you need to create a category in the same project to define your TOOL Part.</p> <p>1. Use Web Part Templates to add a class to your Tool Part.</p> <p>Use the ToolPart template to create a code.</p> <p>2. Add a class to your Tool Part</p> <p>Use Web Custom Control.</p> <p>Add a reference to WebPartPages</p> <p>Using Microsoft.SharePoint.WebPartPages;</p> <p>Inherit ToolPart class</p> <p>Public Class Toolpart1: Toolpart (3) Overloading ApplyChanges Method</p> <p>code show as below</p> <p>/ / -------------------------------------------------------------------------------------------- ---------</p> <p>// file: Toolpart1.cs</p> <p>//</p> <p>// purpose: a Sample Tool Part That Sets the Customtext</p> <p>// Property of WebPart1.</p> <p>/ / -------------------------------------------------------------------------------------------- ---------</p> <p>Using system;</p> <p>Using system.Web.ui;</p> <p>Using system.Web.ui.webcontrols;</p> <p>USING Microsoft.SharePoint;</p> <p>Using Microsoft.SharePoint.WebPartPages;</p> <p>Namespace WebPartLibrary2</p> <p>{</p> <p>/// <summary></p> <p>/// description of the toolpart. Override the gettoolparts method</p> <p>/// in Your Web Part Class to Invoke this Toolpart. To Establish</p> <p>/// a reason to the Web Part the user Has Selected, Uses USE THE</p> <p>/// ParentToolPane.selectedWebPart Property.</p> <p>/// </ summary></p> <p>Public Class Toolpart1:</p> <p>Microsoft.SharePoint.WebPartPages.toolpart</p> <p>{</p> <p>// Declaring a Sample Varialble</p> <p>Private string infutname;</p> <p>// An Event Handler for the init event</p> <p>Private void Toolpart1_init (Object Sender,</p> <p>System.Eventargs E)</p> <p>{</p> <p>InputName = this.uniqueID "Message";</p> <p>}</p> <p>/// <summary></p> <p>/// Constructor for the class. A Great Place To Set Set</p> <p>/// default values ​​for Additional Base Class Properties</p> <p>/// here.</p> <p>/// <summary></p> <p>Public ToolPart1 ()</p> <p>{</p> <p>// SET Default Properties</p> <p>this.title = "Custom Text";</p> <p>THISINIT = New EventHandler (ToolPart1_init);</p> <p>}</p> <p>/// <summary></p> <p>/// Called by the Tool Pane to Apply Property Changes To</p> <p>/// The successd Web Part.</p> <p>/// </ summary></p> <p>Public override void applychanges ()</p> <p>{</p> <p>SimpleWebPart WP1 = (SimpleWebPart)</p> <p>This.ParentToolpane.selectedWebpart;</p> <p>// dend the customer text to the Web Part.</p> <p>WP1.TEXT = Page.Request.form [InputName];</p> <p>}</p> <p>/// <summary> /// if the applychanges method succeeds, this method is</p> <p>/// Called by the Tool Pane to Refresh The Specified</p> <p>/// Property VALUES in The Toolpart User Interface.</p> <p>/// </ summary></p> <p>Public override void syncchanges ()</p> <p>{</p> <p>// sync with the new printy Changes Here</p> <p>}</p> <p>/// <summary></p> <p>/// Called by The Tool Pane If The User Discards Changes</p> <p>/// TO The SELECTED Web Part.</p> <p>/// </ summary></p> <p>Public override void cancelchanges ()</p> <p>{</p> <p>}</p> <p>/// <summary></p> <p>/// render this Tool Part to the Output Parameter</p> <p>/// specified.</p> <p>/// </ summary></p> <p>/// <param name = "Output"></p> <p>/// The HTML Writer to Write Out To</p> <p>/// </ param></p> <p>Protected Override Void RendertoolPart (HTMLTextWriter Output)</p> <p>{</p> <p>// establish a reference to the web part.</p> <p>SimpleWebPart WP1 = (SimpleWebPart)</p> <p>This.ParentToolpane.selectedWebpart;</p> <p>Output.write ("Enter your Custom Text:");</p> <p>Output.write ("<i n p u T name = '" inputname);</p> <p>Output.write ("'type =' text 'value ='" spencode.htmlencode (wp1.text) "'> <br>");</p> <p>}</p> <p>}</p> <p>}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-113282.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="113282" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.051</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'JoOQRMjHWRSkOOJfHh0lO5Np2b5jkpd_2FkWeRST51TVJmEDQi_2B_2FQbxXfncRPE3JS0yTl85b8FKnrQ8aJvojMCqA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>