为了更好的去重用高级查询控件,将此控件做成用户控件。利用VS2005新建一个用控件扩展名为.ascx的文件,参考下图: 
本高级查询的原理是:当用户选择选择内容时,相应的筛选条件,筛选范围进行动态的变化。将筛选条件保存在lblFilter标签中。利用DataView的RowFilter,对数据进行筛选,但为了更好的管理,将这些配置信息保存在表中。注:以下是对应的html源代码:

<%...@ Control Language="C#" AutoEventWireup="true" CodeFile="Advance_Query.ascx.cs"
Inherits="Public_AdvanceQuery" %>
<br />
<table border="1" bordercolor="#ffffff" cellpadding="0" cellspacing="1" width="100%">
<tr>
<td style="height: 15px">
筛选内容:</td>
<td style="height: 15px">
<asp:DropDownList ID="ddlContent" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlContent_SelectedIndexChanged">
</asp:DropDownList></td>
<td style="height: 15px">
筛选条件:</td>
<td style="height: 15px">
<asp:DropDownList ID="ddlCondition" runat="server">
</asp:DropDownList></td>
<td style="height: 15px">
筛选范围:</td>
<td style="height: 15px">
<asp:DropDownList ID="ddlArea" runat="server">
</asp:DropDownList>
<asp:TextBox ID="txtArea" runat="server"></asp:TextBox>
<asp:Label ID="lblFilter" runat="server" Visible="False"></asp:Label>
</td>
<td style="height: 15px">
<asp:Button ID="btnSel" runat="server" OnClick="btnSel_Click" Text="筛选" />
<asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click"
Text="取消" /></td>
</tr>
</table>
<br />以下是本页面对应的后台代码,主要是事件,属性,方法
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
/**//// <summary>
/// 高级查询用户控件
/// 创建人:AAA
/// 创建日期:2006-08-29
/// </summary>
public partial class Public_AdvanceQuery : System.Web.UI.UserControl
...{
//定义类
WebClass.Query query;
protected void Page_Load(object sender, EventArgs e)
...{
if (!Page.IsPostBack)
...{
this._gv.DataSource = this._dv;
this._gv.DataBind();
//设置筛选内容
this.Load_ddlContent();
}
}
protected void ddlContent_SelectedIndexChanged(object sender, EventArgs e)
...{
query = new WebClass.Query(this._form);
if (query.Get_Content_Flag(this.ddlContent.SelectedValue) == "0")
...{
this.ddlArea.Items.Clear();
this.ddlArea.DataSource = query.DataSet_Area(ddlContent.SelectedValue);
this.ddlArea.DataTextField = "Name";
this.ddlArea.DataValueField = "Code";
this.ddlArea.DataBind();
//文本框不可见
this.txtArea.Visible = false;
this.ddlArea.Visible = true;
}
else
...{
this.txtArea.Text = "";
this.txtArea.Visible = true;
this.ddlArea.Visible = false;
}
query.Set_Condition(this.ddlContent.SelectedValue, this.ddlCondition);
}
protected void btnSel_Click(object sender, EventArgs e)
...{
query = new WebClass.Query(this._form);
//将筛选范围的值存储在文本框里
if (this.txtArea.Text == "")
this.txtArea.Text = this.ddlArea.SelectedValue;
if (btnSel.Text == "筛选")
...{
this.btnSel.Text = "继续进行筛选";
this.btnCancel.Text = "取消筛选";
}
if (this.lblFilter.Text == "")
this.lblFilter.Text = query.Filter(this.ddlContent.SelectedValue, this.ddlCondition.SelectedValue, this.txtArea.Text);
else
this.lblFilter.Text += " AND " + query.Filter(this.ddlContent.SelectedValue, this.ddlCondition.SelectedValue, this.txtArea.Text);
//定义DataView
DataView dv = new DataView();
dv = this._dv;
dv.RowFilter = this.lblFilter.Text;
this._gv.DataSource = dv;
this._gv.DataBind();
//清空文本框里的内容
this.txtArea.Text = "";
}
protected void btnCancel_Click(object sender, EventArgs e)
...{
DataView dv = new DataView();
this.btnSel.Text = "筛选";
this.btnCancel.Text = "取消";
this.lblFilter.Text = "";
dv = this._dv;
this._gv.DataSource = dv;
this._gv.DataBind();
}
public void Load_ddlContent()
...{
query = new WebClass.Query(this._form);
//设置筛选内容
this.ddlContent.Items.Clear();
this.ddlContent.DataSource = query.DataSet_Content();
this.ddlContent.DataTextField = "By_Name";
this.ddlContent.DataValueField = "Field";
this.ddlContent.DataBind();
//设置筛选条件
query.Set_Condition(ddlContent.SelectedValue, this.ddlCondition);
//设置筛选范围
if (query.Get_Content_Flag(ddlContent.SelectedValue) == "0")
...{
this.ddlArea.Items.Clear();
this.ddlArea.DataSource = query.DataSet_Area(ddlContent.SelectedValue);
this.ddlArea.DataTextField = "Name";
this.ddlArea.DataValueField = "Code";
this.ddlArea.DataBind();
//文本框不可见
this.txtArea.Visible = false;
this.ddlArea.Visible = true;
}
else
...{
this.txtArea.Text = "";
this.txtArea.Visible = true;
this.ddlArea.Visible = false;
}
query.Set_Condition(this.ddlContent.SelectedValue, this.ddlCondition);
}
/**//// <summary>
/// 当前的窗体
/// </summary>
private string _form;
/**//// <summary>
/// 数据源
/// 注:数据类型 DataView
/// </summary>
private DataView _dv;
/**//// <summary>
/// GridView控件
/// </summary>
private GridView _gv;
/**//// <summary>
/// 设置窗体名称
/// 注:只写属性
/// </summary>
public string strForm
...{
set
...{
this._form = value;
}
}
/**//// <summary>
/// 设置查询的数据源
/// 注:只写属性 类型:DataView
/// </summary>
public DataView DataSource
...{
set
...{
this._dv = value;
}
}
/**//// <summary>
/// 设置GridView控件
/// </summary>
public GridView GridViewID
...{
set
...{
this._gv = value;
}
}
/**//// <summary>
/// 获取筛选字符串/设置筛选条件
/// </summary>
public string Filter
...{
get
...{
if (this.lblFilter.Text == "")
return "";
else
return this.lblFilter.Text;
}
set
...{
this.lblFilter.Text = value;
}
}
public delegate void ButtonClickEventHandler(object sender, System.EventArgs e);
//public event ButtonClickEventHandler SelClick;
public delegate void eventhandler(object sender, EventArgs e);
//public event eventhandler CalClick;
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebClass
...{
/**//// <summary>
/// 高级查询
/// 书写人:AAA
/// 创建日期:2006-08-28
/// </summary>
public class Query:DBClass.DBClass
...{
/**//// <summary>
/// 当前的窗体名称
/// </summary>
private string _strQueryForm;
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="QueryForm">高级查询对应的窗体</param>
/// <param name="ECode">当前的企业编号</param>
public Query (string QueryForm)
...{
_strQueryForm = QueryForm;
}
/**//// <summary>
/// 获取当前窗体对应的筛选内容的数据集
/// </summary>
/// <returns>数据集</returns>
public DataSet DataSet_Content()
...{
string mysql = string.Format("SELECT By_Name,Field FROM Advance_Query WHERE form='{0}'", this._strQueryForm);
return base.ExecuteSqlDs(mysql, "Content");
}
/**//// <summary>
/// 设置筛选条件
/// </summary>
///&nb