.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Creating a Visual Web Part with Custom Properties

Posted By:abhays       Posted Date: October 30, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

This programming describes how to create a Web Part with custom properties. The Web Part base class contains a set of properties that can be used to control its appearance and behaviour like Height, Width, and Chrome Type etc. A custom property is a property that you create when you need additional properties to enhance your Web Part's functionality.

Follow below steps to create Visual Web Part

  • Create a new SharePoint 2010 Empty SharePoint Project.
  • Click on the project, choose Add > New Item.
  • Choose the Visual Web Part template and name it ListWP.

Modify ListWP.cs as below public class ListWP :

    public class ListWP : WebPart
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/AS.SPD.ListWP/ListWP/ListWPUserControl.ascx";

        private string listName;

        [WebBrowsable(true), WebDisplayName("ListName"), WebDescription("List Name"),
         Personalizable(PersonalizationScope.Shared), Category("Custom Property"),
        public string ListName
            get { return listName; }
            set { listName = value; }

        //protected override void CreateChildControls()
        //    Control control = Page.LoadControl(_ascxPath);
        //    Controls.Add(control);

        protected override void CreateChildControls()
            Control control = Page.LoadControl(_ascxPath);
            ListWPUserControl uc = (ListWPUserControl)control;
            uc.ListName = this.ListName;


Add GridView code in ListWPUserControl.ascx

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="true">

Add below code in ListWPUserControl.ascx.cs

 public partial class ListWPUserControl : UserControl
        private string listName="Country";
        public string ListName
            get { return listName; }
            set { listName = value; }
        protected void Page_Load(object sender, EventArgs e)
            if (!string.IsNullOrEmpty(ListName))

                SPSite siteCol = SPContext.Current.Site;
                SPWeb web = siteCol.OpenWeb();
                //SPList list = web.GetList(listName);
                SPList list = web.Lists[ListName];
                //SPListItemCollection items = list.GetItems("ID","Title");
                DataTable dt = list.Items.GetDataTable();
                gv.DataSource = dt;

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend