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);
}
}
}