SharePoint 2010 Custom Delegate Control

Source: Internet
Author: User
Tags visual studio visual studio 2010

Start the SharePoint development tool in Microsoft Visual Studio 2010.

On the File menu, point to New, and then click Project.

In project type, under Visual Basic or C #, select Blank SharePoint project.

Type Ecmascriptdelegate as the project name. Click OK.

In the SharePoint Customization wizard, select Deploy as Farm solution. Click Finish.

Right-click the Ecmascriptdelegate project in Solution Explorer. Select Add and new items in turn.

In the Add New Project dialog box, click the code group and select the class template. Type Ecmascriptdelegatecontrol as the name, and then click Add.

Next, you must add a reference to the system.web. In Solution Explorer, right-click the References folder and choose Add Reference. In the Add Reference dialog box, click the. NET tab and find "system.web" in the list. Click OK.

In the Ecmascriptdelegatecontrol file that appears, add the following using statement.

Using System.Web.UI.WebControls;

Change the Ecmascriptdelegatecontrol base class to WebControl by modifying the following row.

Class Ecmascriptdelegatecontrol:webcontrol

Rewrite the OnLoad method by adding the following code.

Protectedoverridevoid OnLoad (EventArgs e)
{
  base. OnLoad (e);
}

In the OnLoad method, add the following code to add ECMAScript to the page.

String Helloalert = "alert (' Hello, world! ');";
  This. Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "popup", Helloalert, True);

You have now generated a delegate control for your project. Next, you will create a feature to deploy the control.

Creating features to deploy controls

In Solution Explorer, right-click the Ecmascriptdelegate project, and select Add and new items in turn.

In the Add New Project dialog box, select the empty element template and type Ecmascriptdelegatefeature as the name. Click Add.

Insert the following XML in the Elements element. The Id attribute identifies the delegate that renders the control. The controlassembly and Controlclass properties are unique to your control.

<control id= "Additionalpagehead" controlassembly= "Ecmascriptdelegate, version=1.0.0.0, Culture=neutral, Publickeytoken=public Key Token "controlclass=" Ecmascriptdelegate.ecmascriptdelegatecontrol ">

You now have the functionality you need to control and deploy the control. Control requires a SafeControl entry in the Web.config file to run on the page. The following procedure adds a SafeControl entry for your control.

Add SafeControl Item

In Solution Explorer, click Ecmascriptdelegatefeature, and in the security Control entry property, click ....

In the Security Control Entry dialog box, click Add.

In the Properties box, make sure that the Namespace property has the correct value. It should be the namespace of your control. Also, make sure that the security property is set to true. Click OK.

Now that you have added the SafeControl entry, you can deploy the solution.

Deploying and testing Delegate controls

Press F5 to run the solution.

When the page is loaded, a display of "Hello, world!" appears. The dialog box. This is the script that the delegate control has added to the page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.