Click here to Skip to main content
15,845,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a custom webpart in which a added a toolpart to choose the datasource of the grid in the webpart.I am getting the dropdown list in the toolpart but can't figure out how to bind it to the grid in my webpart.I get a "null reference exception" when i populate the grid.Please help in resolving.
C#
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;
using Microsoft.SharePoint.WebPartPages;
using System.Data;

namespace BHPB.WorkSpaces.Common.WebParts.CustomList
{
    [ToolboxItemAttribute(false)]
    public class CustomList : Microsoft.SharePoint.WebPartPages.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/BHPB.WorkSpaces.Common.WebParts/CustomList/CustomListUserControl.ascx";
        public GridView Property1;
        [Category("List Settings")]
        [WebBrowsable(true)]
        public string Property2
        {
            get
            {
                return _property2;
            }
            set
            {
                _property2 = value;
            }
        }
        string _property2;

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            writer.Write(Property1);
            writer.Write("<br/>");
            writer.Write(Property2);
        }

        public override ToolPart[] GetToolParts() 
        { 
        ToolPart[] allToolParts = new ToolPart[3]; 
        WebPartToolPart standardToolParts = new WebPartToolPart(); 
        CustomPropertyToolPart customToolParts = new CustomPropertyToolPart();  

        allToolParts[0] = standardToolParts; 
        allToolParts[1] = customToolParts; 
        allToolParts[2] = new CustomToolPart();  

        return allToolParts; 
        }


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

            Controls.Add(control);
        }
    }



    public class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    {
        DropDownList ddl;
        Panel toolPartPanel;
        TextBox tb;

        protected override void CreateChildControls()
        {
            toolPartPanel = new Panel();
            ddl = new DropDownList();
            ddl.ID = "ddl";

            // Simply getting the lists of the current web, and displaying them in the dropdown-list. 
            SPListCollection lists = SPContext.Current.Web.Lists;
            foreach (SPList list in lists)
                ddl.Items.Add(list.Title);

            tb = new TextBox();
            tb.ID = "tb";

            toolPartPanel.Controls.Add(ddl);
            toolPartPanel.Controls.Add(tb);
            Controls.Add(toolPartPanel);
            base.CreateChildControls();
        }

        public override void ApplyChanges()
        {
            CustomList wp = (CustomList)this.ParentToolPane.SelectedWebPart;
            SPSite site = SPContext.Current.Site;
            SPWeb web = site.RootWeb;            
            SPList list = web.Lists[ddl.SelectedItem.Text];
            SPListItemCollection slist = list.GetItems();
            DataTable dt = new DataTable();
            dt = slist.GetDataTable();
            wp.Property1.DataSource = slist.GetDataTable();
            wp.Property2 = tb.Text;
            wp.Property1.DataBind();
            this.Controls.Add(wp.Property1);
        }
    }



}
Posted
Comments
Guptha Ch 16-Dec-14 9:15am    
Can you update the code in Webpart user control in the question.. how you are accessing it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900