Use vs to create event Handler in standard Web Part parts

Source: Internet
Author: User
Tags visual studio

Creating events for Web Part parts is a central part of building Web Parts parts. This article explains how to use Visual Studio to create event handlers in the standard Web Parts part.

1. Open visual Studio to create a new blank SharePoint Project Spwebpartevent, click OK. Deployed as a farm solution.

2. Right-click the item to add a new Web Part Sampleeventwebpart. Click OK.

3. Open the Sampleeventwebpart.webpart and modify its caption and description properties.

<?xml version= "1.0" encoding= "Utf-8"?>
<webParts>
<webpart xmlns= "http:// Schemas.microsoft.com/webpart/v3 ">
<metaData>
<type name=" SPWebPartEvent.SampleEventWebPart.SampleEventWebPart, $SharePoint. project.assemblyfullname$ "  />
< importerrormessage> $Resources:core,importerrormessage;</importerrormessage>
</metaData>
<data>
<properties>
<property name= "Title" type= "string" >sp Site Lists Web part</ property>
<property name= "Description" type= "string" >list of Lists from SharePoint Site.</property >
</properties>
</data>
</webPart>
</webParts>

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

4. Open SampleEventWebPart.cs and modify the code.

Using System;
Using System.ComponentModel;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using Microsoft.SharePoint;
    
Using Microsoft.SharePoint.WebControls;
namespace Spwebpartevent.sampleeventwebpart {[ToolboxItemAttribute (false)] public class Sampleeventwebpart:webpart {
Be sure to replace Mysiteurl with your server URL.
Make sure to replace the mysiteurl here with the URL of your own server.
String mysiteurl = "http://smallville-pc:1528/";
ListBox mysplists = new ListBox ();
String listinfo = "";
Button getlists = New button (); protected override void OnPreRender (EventArgs e) {getlists.text = "Click to get all lists";} protected override void Createchildcontro LS () {this.
Controls.Add (getlists); This.
Controls.Add (mysplists);
Getlists.click + = new EventHandler (Getlists_click);  } void Getlists_click (object sender, EventArgs e) {using (SPSite mysitecollection = new SPSite (Mysiteurl)) {using (SPWeb Myspsite = Mysitecollection.rootweb) {foreach (SPList myList inmyspsite.lists) {listinfo = MyList.Title.ToString (); MySPLists.Items.Add (Listinfo);}} }
}
}
}

5. Click Build now-Deploy the solution.

6. On the SharePoint site, click Site Actions-Edit pages-Add Web Parts, select SP Site Lists Web Part in the custom class, and click Add.

7. Try clicking the button "Get all list" and you can see:

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.