Web Parts

Web Parts  Sharepoint 2010
 Creating Basic Web part using Visual Studio 2010.
Step 1)  Create a Project Called "WebPartDemo1" using VS 2010(Select .NET Framerwork 3.5)
             Choose "Empty Sharepoint Project"





Step2) Enter Site URL (in this case http://sp2010:21016/) and select "Deploy as a Farm Solution"--> Click FINISH













































Step 3)  Select Project "WebpartDemo1" right click add new item-->select Webpart(as shown below)


Name this webpart as "MyWebpart"-->Click OK





Webpart code will be added to project
It has 3 files 1) Elements.xml
2) MyWebpart.cs
3) MyWebpart.webpart

Step 4)  Open MyWebpart.cs
  add few labels and  button as shown below.

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 webpartdemo1.MyWebpart
{
    [ToolboxItemAttribute(false)]
    public class MyWebpart : WebPart
    {
//2 lables  & 1 button control 
        Label label1 = new Label();
        Label label2 = new Label();
        Button btn = new Button();
        protected override void CreateChildControls()
        {
            label1.Text = "This is my ffirst webpart demo\r\n";
            label2.Text = "Button click will update my content\r\n";
            btn.Click += new EventHandler(btn_Click);
            btn.Width = 200;
            btn.Height = 50;
            btn.Text = "CLick me..";

            
            this.Controls.Add(label1);
            
            this.Controls.Add(label2);
            
            this.Controls.Add(btn);



        }

        void btn_Click(object sender, EventArgs e)
        {
            label2.Text = "User clicked button";
        }
    }
}





Step 5) Build and deploy as shown below




Step 6)  Now to go site http://sp2010:21016(in this case)


Edit any webpage--> CLick on "Insert" tab --> Click on "WebPart"-->Select "Custom" under categories--> Ur webpart  named "MyWebpart" ,select it -->CLick on Add.(as shown below)

OUTPUT
   Click on Button, Label2 text will update.

   

No comments:

Post a Comment