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: