Well, users are always right. So my job is to create a similar web part.
As this web part will connect to other web parts to provide filter function, first of all, the web part should inherit from ITransformableFilterValues.
public class SharePointListFilterWebPart : Microsoft.SharePoint.WebPartPages.WebPart, ITransformableFilterValues
Then override CreateChildControls.
try
{
base.CreateChildControls();
_values = new System.Web.UI.WebControls.RadioButtonList();
//Get all items from the list and add to the radio button control;
//Listname is a property so when configure the webpart, user can assign the list.
SPWeb web = Microsoft.SharePoint.SPContext.Current.Web;
SPList list = web.Lists[ListName];
SPListItemCollection listItems = list.Items;
if(listItems.Count > 0)
{
foreach (SPListItem listItem in listItems)
{
_values.Items.Add(new ListItem(listItem[ColumnName].ToString()));
}
//Default select the first item
if (!Page.IsPostBack)
_values.Items[0].Selected = true;
//auto postback when the user select different item.
_values.AutoPostBack = true;
this.Controls.Add(_values);
}
else
{
this.Controls.Add(new LiteralControl("There are no items in the list"));
}
}
catch (Exception ex)
{
//Exception handle
}
Then implement members in ITransformableFilterValues
public bool AllowEmptyValue { get { return false; } }
public bool AllowAllValue { get { return true; } }
public bool AllowMultipleValues { get { return true; } }
public string ParameterName { get { return ColumnName; } }
Add the following code to define the connection and the values to transfor.
public System.Collections.ObjectModel.ReadOnlyCollection
{
get
{
bool selected = false;
System.Collections.Generic.List
for (int i = 0; i < _values.Items.Count; i++)
{
if (_values.Items[i].Selected)
{
values.Add(_values.Items[i].Value);
//for radion button
selected = true;
break;
}
}
if (!selected)
values.Add(_values.Items[0].Value);
System.Collections.ObjectModel.ReadOnlyCollection
return result;
}
}
[ConnectionProvider(
"List",
"UniqueIDForListConnection",
AllowsMultipleConnections = true)]
public Microsoft.SharePoint.WebPartPages.ITransformableFilterValues SetConnection()
{
return this;
}
This is just a piece of my code. It’s better to install WSP Builder for Visual Studio which can create web part template and build SharePoint solution (*.wsp) easily.
No comments:
Post a Comment