using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace AdminControls { public class SearchAndPage : Table { // a delegate needed to return a datatable public delegate DataTable RetrieveDataHandler(); public delegate WebControl GetInputControlHandler(string DataField); // sub controls protected Button btnSearch; // public properties private string _TargetGridViewID = null; public string TargetGridViewID { get { return _TargetGridViewID; } set { _TargetGridViewID = value; } } public event RetrieveDataHandler RetrieveData; public event GetInputControlHandler GetInputControl; // private properties private string GridViewSortDirection { get { return ViewState["SortDirection"] as string ?? "ASC"; } set { ViewState["SortDirection"] = value; } } private string GridViewSortExpression { get { return ViewState["SortExpression"] as string ?? string.Empty; } set { ViewState["SortExpression"] = value; } } private string SearchQuery { get { return ViewState["SearchQuery"] as string ?? string.Empty; } set { ViewState["SearchQuery"] = value; } } private string GetSortDirection() { switch (GridViewSortDirection) { case "ASC": GridViewSortDirection = "DESC"; break; case "DESC": GridViewSortDirection = "ASC"; break; } return GridViewSortDirection; } // internal globals private GridView TargetGridView; protected override void OnInit(EventArgs e) { if (_TargetGridViewID != null) { #region set subcontrols and this table this.CellPadding = 1; this.CellSpacing = 0; #endregion #region set up search TargetGridView = (GridView)Parent.FindControl(_TargetGridViewID); if (TargetGridView == null) throw new Exception("TargetGridViewID does not reference a valid GridView control."); if (TargetGridView.AllowPaging == false || TargetGridView.AllowSorting == false) throw new Exception("Please set AllowPaging and AllowSorting on the target GridView to 'True'."); btnSearch = new Button(); btnSearch.Text = "Search"; btnSearch.Click += new EventHandler(btnSearch_Click); #endregion #region set up paging TargetGridView.Sorting += new GridViewSortEventHandler(TargetGridView_Sorting); TargetGridView.PageIndexChanging += new GridViewPageEventHandler(TargetGridView_PageIndexChanging); #endregion // now lets set up our table TableRow labelRow = new TableRow(); TableRow fieldRow = new TableRow(); for (int i = 0; i < TargetGridView.Columns.Count; i++) { // get the datacontrolfield for simplicity DataControlField dcf = (DataControlField)TargetGridView.Columns[i]; // create the label cell and fill it with the header text of the column TableCell labelCell = new TableCell(); labelCell.Text = dcf.HeaderText; labelRow.Cells.Add(labelCell); // craete the field cell and add specified control to it TableCell fieldCell = new TableCell(); WebControl wcField; if (GetInputControl != null) wcField = GetInputControl(dcf.SortExpression); else wcField = new TextBox(); if (wcField != null) { // set the width of this field to the column width if (dcf.ItemStyle.Width.Value > 0) wcField.Width = Unit.Pixel((int)dcf.ItemStyle.Width.Value - 5); // add n stuff fieldCell.Controls.Add(wcField); } fieldRow.Cells.Add(fieldCell); } // finally add the search button labelRow.Cells.Add(new TableCell()); TableCell searchCell = new TableCell(); searchCell.Controls.Add(btnSearch); fieldRow.Cells.Add(searchCell); this.Rows.Add(labelRow); this.Rows.Add(fieldRow); } base.OnInit(e); } public void BindData() { TargetGridView.DataSource = RunSearch(false); TargetGridView.DataBind(); } protected void TargetGridView_Sorting(object sender, GridViewSortEventArgs e) { GridViewSortExpression = e.SortExpression; int pageIndex = TargetGridView.PageIndex; TargetGridView.DataSource = RunSearch(false); TargetGridView.DataBind(); TargetGridView.PageIndex = pageIndex; } protected void TargetGridView_PageIndexChanging(object sender, GridViewPageEventArgs e) { TargetGridView.DataSource = RunSearch(true); TargetGridView.PageIndex = e.NewPageIndex; TargetGridView.DataBind(); } protected void btnSearch_Click(object sender, EventArgs e) { string search = ""; for (int i = 0; i < TargetGridView.Columns.Count; i++) { DataControlField dcf = (DataControlField)TargetGridView.Columns[i]; if (Rows[1].Cells[i].Controls.Count > 0) { WebControl wcField = (WebControl)this.Rows[1].Cells[i].Controls[0]; if (wcField is TextBox) { string TextVal = ((TextBox)wcField).Text; if (TextVal.Length > 0) { if (search.Length > 0) search += "AND"; search += dcf.SortExpression + " LIKE '%" + TextVal + "%' "; } } else if (wcField is DropDownList) { string ddlVal = ((DropDownList)wcField).SelectedValue; if (ddlVal.Length > 0) { if (search.Length > 0) search += "AND "; int intVal; if (Int32.TryParse(ddlVal, out intVal)) search += dcf.SortExpression + " = " + ddlVal + " "; else search += dcf.SortExpression + " = '" + ddlVal + "' "; } } else if (wcField is CheckBox) { bool check = ((CheckBox)wcField).Checked; if (search.Length > 0) search += "AND "; search += dcf.SortExpression + " = " +(check ? "1 " : "0 "); } else throw new Exception("Found a control of type " + wcField.GetType().ToString() + ". Cannot determine it's value."); } } //throw new Exception(search); SearchQuery = search; BindData(); } protected DataView RunSearch(bool isPageIndexChanging) { DataTable dt = RetrieveData(); if (dt != null) { DataView dataView = new DataView(dt); if (GridViewSortExpression != string.Empty) { if (isPageIndexChanging) dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection); else dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection()); } if (SearchQuery != string.Empty) { dataView.RowFilter = SearchQuery; } return dataView; } else return new DataView(); } } }