0
Answer

Issue in displaying list in custom webpart +custom toolpart!

Ralph Cuisak

Ralph Cuisak

9y
544
1
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.Here is my webpart.cs file

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 BH.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/BH.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);

}

}

}