高级查询--asp.net(C#)

为了更好的去重用高级查询控件,将此控件做成用户控件。利用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" >
                
&nbsp; < 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>
        
/// <param name="content">当前筛选内容的Value值</param>
        
/// <param name="ddlCondition">当前筛选条件对应的下拉框控件的ID</param>

        public void Set_Condition(string content,DropDownList ddlCondition)
        
{
            
switch (Get_Content_Type(content))
            
{
                
case "S":
                    
switch (Get_Content_Flag(content))
                    
{
                        
case "1":
                            ddlCondition.Items.Clear();
                            ddlCondition.Items.Add(
new ListItem("包含"" = "));
                            
break;
                        
case "0":
                            ddlCondition.Items.Clear();
                            ddlCondition.Items.Add(
new ListItem("包含"" = "));
                            ddlCondition.Items.Add(
new ListItem("不包含"" <> "));
                            
break;
                    }

                    
break;
                
case "D":
                    ddlCondition.Items.Clear();
                    ddlCondition.Items.Add(
new ListItem("等于"" = "));
                    ddlCondition.Items.Add(
new ListItem("大于"" > "));
                    ddlCondition.Items.Add(
new ListItem("大于等于"" >= "));
                    ddlCondition.Items.Add(
new ListItem("小于"" < "));
                    ddlCondition.Items.Add(
new ListItem("小于等于"" <= "));
                    ddlCondition.Items.Add(
new ListItem("不等于"" <> "));
                    
break;
                
case "N":
                    ddlCondition.Items.Clear();
                    ddlCondition.Items.Add(
new ListItem("等于"" = "));
                    ddlCondition.Items.Add(
new ListItem("大于"" > "));
                    ddlCondition.Items.Add(
new ListItem("大于等于"" >= "));
                    ddlCondition.Items.Add(
new ListItem("小于"" < "));
                    ddlCondition.Items.Add(
new ListItem("小于等于"" <= "));
                    ddlCondition.Items.Add(
new ListItem("不等于"" <> "));
                    
break;
            }

        }

        
/// <summary>
        
/// 获取当前筛选内容对应的类型
        
/// 注:D:日期类型 N:数值类型 S:字符类型
        
/// </summary>
        
/// <param name="content">当前筛选内容的Value值</param>
        
/// <returns>筛选内容的类型</returns>

        private string Get_Content_Type(string content)
        
{
            
string strSql = string.Format("SELECT Type FROM Advance_Query WHERE Form='{0}' AND Field='{1}'",
                
this._strQueryForm,content);
            
return base.ExecuteSqlValue(strSql).ToString();
        }

        
/// <summary>
        
/// 获取当前筛选内容对应的筛选范围
        
/// </summary>
        
/// <param name="content">当前筛选内容的Value值</param>
        
/// <returns>数据集</returns>

        public DataSet DataSet_Area(string content)
        
{
            
if (Get_Content_Flag(content)=="0")
            
{
                
string strSql = string.Format("SELECT AreaSQL FROM Advance_Query WHERE Form='{0}' AND Field='{1}'",
                
this._strQueryForm,content);
                
return base.ExecuteSqlDs(base.ExecuteSqlValue(strSql).ToString(),"area");
            }

            
else
                
return null;
        }

        
/// <summary>
        
/// 获取筛选内容对应的标记
        
/// 注:0:利用的是下拉框;1:利用的是文本框
        
/// </summary>
        
/// <param name="content">当前选择的筛选内容的Value值</param>
        
/// <returns>字符串 0:利用的是下拉框;1:利用的是文本框</returns>

        public string Get_Content_Flag(string content)
        
{
            
//查询语句,判断数据库中该字段对应的标志信息
            string strSql = string.Format("SELECT Flag From Advance_Query WHERE Form='{0}' AND Field='{1}'",
                
this._strQueryForm,content);
            
return base.ExecuteSqlValue(strSql).ToString();
        }

        
/// <summary>
        
/// 获取筛选字符串
        
/// </summary>
        
/// <param name="content">筛选内容</param>
        
/// <param name="Condition">筛选条件</param>
        
/// <param name="myValue">筛选范围</param>
        
/// <returns>字符串</returns>

        public string Filter(string content, string Condition, string myValue)
        
{
            
switch (Get_Content_Type(content))
            
{
                
case "S":
                    
{
                        
if (this.Get_Content_Flag(content) == "0")
                        
{//当为下拉框时,进行全匹配
                            return string.Format("{0}{1}'{2}'", content, Condition, myValue);
                        }

                        
else
                        
{//当为文本框时,进行内容部分匹配
                            return string.Format("{0} LIKE '%{1}%'", content, myValue);
                        }

                    }

                
case "N":
                    
return string.Format("{0}{1}{2}", content,Condition,myValue);
                
case "D":
                    
return string.Format("{0}{1}'{2}'", content,Condition,myValue);
                
default:
                    
return string.Format("{0}{1}'{2}'", content,Condition,myValue);
            }

        }

        
/// <summary>
        
/// 获取进行查询的表名称
        
/// </summary>
        
/// <param name="content">筛选内容</param>
        
/// <returns>需要进行查询的表名称</returns>

        private string Get_Tabel_Name(string content)
        
{
            
string strSql = string.Format("SELECT tbl FROM Advance_Query WHERE FORM='{0}' AND field='{1}'"this._strQueryForm, content);
            
return base.ExecuteSqlValue(strSql).ToString();
        }

        
/// <summary>
        
/// 获取DateView
        
/// </summary>
        
/// <returns></returns>

        public DataView Dv_SQl(string content)
        
{
            
return base.ExecuteSqlDs(string.Format("SELECT * FROM {0}"this.Get_Tabel_Name(content)),"SR_EOS").Tables[0].DefaultView;
        }

    }

}

      在本类库需要调用最基层的DBClass,是对数据库的最基村的操作,主要是进行读取数据,获取数据集的操作,在这里,不做详细书写。
       经过编译成功后,新建网页,将此用户控件拖动到需要进行的页面。进行以下的编码,主要是设置GridView控件的ID,数据源注意使DataView,在这里可以将DataSet转换成DataView,可以在页面上一个按钮,高级查询,单击后,加载用户控件如下图所示:
高级查询--asp.net(C#)_第1张图片
我们可以看到,在它的下面是个GridView控件
这后我们对PageLoad,和图片中的高级查询按钮进行编写代码,如下:

public  WebClass.Customer myCustomer  =   new  WebClass.Customer();
protected   void  Page_Load( object  sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            
this.tvCustomer.Nodes.Add(new WebClass.TreeShow().CustomerType());
            
this.gvCustomer.DataSource = myCustomer.DataSet_Customer();
            
this.gvCustomer.DataBind();
            gvStat.DataSource 
= myCustomer.DataSet_Customer_Stat();
            gvStat.DataBind();
            
this.btnAdd.Attributes.Add("OnClick""window.showModalDialog('customeradd.aspx'),'','resizable:yes;status:no;'");
        }

        Advance_Query1.strForm 
= "Customer";
        Advance_Query1.GridViewID 
= this.gvCustomer;
        Advance_Query1.DataSource 
= this.dv("");
    }

... ...
protected   void  btnAdvSel_Click( object  sender, EventArgs e)
    
{
        
if (this.Panel1.Visible)
        
{
            
this.Panel1.Visible = false;
            
this.btnAdvSel.Text = "显示高级查询";
            
this.gvCustomer.DataSource = myCustomer.DataSet_Customer();
            
this.gvCustomer.DataBind();
        }

        
else
        
{
            
this.Panel1.Visible = true;
            
this.btnAdvSel.Text = "隐藏高级查询";
        }

    }

注:本高级查询的配置信息存储在数据表中(Advance_Query):

DROP   TABLE  Advance_Query;
CREATE   TABLE  Advance_Query
(
    Tbl            
VARCHAR2 ( 30 )     NOT   NULL ,     -- 表名称    PK
    Form             VARCHAR2 ( 30 )     NOT   NULL ,     -- 所属窗体名称    pk
    Field             VARCHAR2 ( 30 )     NOT   NULL ,     -- 字段名称    pk
    By_Name             VARCHAR2 ( 30 )     NOT   NULL ,     -- 字段别名
    Type             VARCHAR2 ( 1 )     NOT   NULL ,     -- 字段类型名称
    Flag             VARCHAR2 ( 1 )     NOT   NULL ,     -- 标示  0:利用下拉框 1:用文本框
    AreaSQL             VARCHAR2 ( 150 ),         -- 当前值对应的查询语句 适合于:flag=0时使用
     CONSTRAINT  PK_Text  PRIMARY   KEY (Tbl,Form,Field)
);

这样,运行的你的页面,就可以做到高级查询。

你可能感兴趣的:(String,object,server,C#,query,asp)