Export (0) Print
Expand All

How to: Write a Multi-Select CheckBox Filter Web Part

SharePoint 2007
  1. In this topic, let's look at creating a simple filter Web Part that can provide filter values to other web parts. The sample Region CheckBox filter Web Part allows the user to filter data based on the regions the user selects. The user interface (UI) for the filter contains check boxes and allows multiple selections.

  2. Other than creating a user interface with check-box controls, the steps we need to follow to create a simple filter provider Web Part are:

  3. Implement the ITransformableFilterValues interface to connect this Web Part with the consumer Web Parts.

  4. Expose a Provider connection point to return the IFilterValues interface instance with the values the user selected.

Here's the sample code for the Filter Web Part. Follow the steps in the Walkthrough: Writing a Simple Filter Consumer Web Part Sample to deploy this Web Part.

Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using aspnetwebparts = System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Server.Utilities;
using wsswebparts = Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Portal.WebControls;
using System.Collections.ObjectModel;
using Microsoft.SharePoint.Utilities;
using System.Data;
using System.Collections;

namespace MyWebPartLibrary
{
    public class RegionFilterWebPart : aspnetwebparts.WebPart, wsswebparts.ITransformableFilterValues
    {
        CheckBoxList cblRegionList;
        ListItem cbitemRegion;

        public virtual bool AllowMultipleValues
        {
            get
            {
                return true;
            }
        }
        public virtual bool AllowAllValue
        {
            get
            {
                return true;
            }
        }

        public virtual bool AllowEmptyValue
        {
            get
            {
                return false;
            }
        }
        public virtual string ParameterName
        {
            get
            {
                return "Geography";
            }
        }

        public virtual ReadOnlyCollection<string> ParameterValues
        {
            get
            {
                string[] values = this.GetCurrentlySelectedGeographies();
                return values == null ?
                    null :
                    new ReadOnlyCollection<string>(values);
            }
        }




        protected override void CreateChildControls()
        {
            cblRegionList = new CheckBoxList();
            Controls.Add(cblRegionList);

            cbitemRegion = new ListItem();
            cbitemRegion.Text = "Seattle";
            cblRegionList.Items.Add(cbitemRegion);
            cbitemRegion = null;

            cbitemRegion = new ListItem();
            cbitemRegion.Text = "US";
            cblRegionList.Items.Add(cbitemRegion);
            cbitemRegion = null;

            cbitemRegion = new ListItem();
            cbitemRegion.Text = "World";
            cblRegionList.Items.Add(cbitemRegion);
            cbitemRegion = null;

            cbitemRegion = new ListItem();
            cbitemRegion.Text = "All";
            cblRegionList.Items.Add(cbitemRegion);
            cbitemRegion = null;


            base.CreateChildControls();
        }


        [aspnetwebparts.ConnectionProvider("Region Filter", "ITransformableFilterValues", AllowsMultipleConnections = true)]
        public wsswebparts.ITransformableFilterValues SetConnectionInterface()
        {
            return this;
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        public string[] GetCurrentlySelectedGeographies()
        {
            String[] choices = new String[5];
            bool anythingSelected = false;

            for (int i = 0; i < cblRegionList.Items.Count; i++)
            {
                //get the selected choices
                if (cblRegionList.Items[i].Selected)
                {
                    anythingSelected = true;
                    if (cblRegionList.Items[i].Text != "All")
                    {
                        choices[i] = cblRegionList.Items[i].Text;
                    }
                    else
                    {
                        choices = null;
                        return choices;
                    }
                }

            }
            if (!anythingSelected)
                choices = null;

            return choices;
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            this.EnsureChildControls();
            RenderChildren(output);


        }

    }
}

See Also

Community Additions

ADD
Show:
© 2014 Microsoft