using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; namespace Estore { public delegate void AttributeSelectedHandler(int AttributeID); public class FilterControl : WebBlocksControl { private class FilterControlItem : CompositeControl { protected Button btnGo; public DropDownList DropDown; protected string Label; public event AttributeSelectedHandler AttributeSelected; public FilterControlItem(string _Label) { Label = _Label; } protected override void OnInit(EventArgs e) { DropDown = new DropDownList(); Controls.Add(DropDown); btnGo = new Button(); btnGo.Text = "Go"; btnGo.Click += new EventHandler(btnGo_Click); Controls.Add(btnGo); } protected void btnGo_Click(object sender, EventArgs e) { if (AttributeSelected != null) AttributeSelected(Int32.Parse(DropDown.SelectedValue)); } protected override void Render(HtmlTextWriter writer) { writer.Write("
"); writer.Write("
"); writer.Write(Label); writer.Write("
"); DropDown.RenderControl(writer); btnGo.RenderControl(writer); writer.Write("
"); } } public int CategoryID { get { return (ViewState["CategoryID"] == null ? 0 : (int)ViewState["CategoryID"]); } set { ViewState["CategoryID"] = value; } } public int AttributeCount { get { return (ViewState["AttributeCount"] == null ? 0 : (int)ViewState["AttributeCount"]); } set { ViewState["AttributeCount"] = value; } } public string AttributeIDList { get { return (string)ViewState["AttributeIDList"]; } set { ViewState["AttributeIDList"] = value; } } public event AttributeSelectedHandler AttributeSelected; public void BindData() { if (CategoryID > 0) { DataTable dt = Database.Attributes_GetValidSubAttributes(CategoryID, AttributeIDList, AttributeCount); string CurrentAttributeGroup = ""; FilterControlItem CurrentFilterControlItem = null; foreach (DataRow dr in dt.Rows) { string GroupName = (string)dr["GroupName"]; if (GroupName != CurrentAttributeGroup) { CurrentAttributeGroup = GroupName; // create a new filter control item CurrentFilterControlItem = new FilterControlItem((string)dr["GroupName"]); CurrentFilterControlItem.AttributeSelected += new AttributeSelectedHandler(CurrentFilterControlItem_AttributeSelected); Controls.Add(CurrentFilterControlItem); } if (!Page.IsPostBack) { CurrentFilterControlItem.DropDown.Items.Add(new ListItem((string)dr["Name"] + "(" + ((int)dr["ProductCount"]).ToString() + ")", dr["AttributeID"].ToString())); } } } } protected void CurrentFilterControlItem_AttributeSelected(int AttributeID) { if (AttributeSelected != null) AttributeSelected(AttributeID); } } }