MSPlus WebControls For ASP.NET. (服务器端控件)

Upgrade Develop Efficiency,Surprise For U! - Mack.Z

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
using System;
using System.IO;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
namespace EnterpriseStandardSet
{
[DefaultProperty("BaseSqlCommand")]
[ToolboxData("<{0}:QueryMaker runat=\"server\" />")]
public class QueryMaker : WebControl,INamingContainer
{
private Table t= new Table();
private Button btnQueryMake=new Button();
public QueryMaker() : base()
{
BaseSqlCommand = "";
ConnectionString = "";
tid=0;
Font.Size=10;
ReceptChildControls=false;
FieldType=new string[]{};
btnQueryMake.Text=" 产生\n\r查询";
}
//数据库连接串
public string ConnectionString
{
get {return Convert.ToString(ViewState["ConnectionString"]);}
set {ViewState["ConnectionString"] = value;}
}
//基本查询sql语句
public string BaseSqlCommand
{
get {return Convert.ToString(ViewState["BaseSqlCommand"]);}
set {ViewState["BaseSqlCommand"] = value;}
}
//最后组装出的完整sql语句
public string FullSqlCommand
{
get {return (string)ViewState["FullSqlCommand"];}
set {ViewState["FullSqlCommand"] = value;}
}
//日期图标路径
public string imgUrl
{
get {return (string)ViewState["imgUrl"];}
set {ViewState["imgUrl"] = value;}
}
//数据库字段集合
private DataTable Fn
{
get {return (DataTable)ViewState["Fn"];}
set {ViewState["Fn"] = value;}
}
//逻辑运算符集合
private DataTable Ls
{
get {return (DataTable)ViewState["Ls"];}
set {ViewState["Ls"] = value;}
}
//运算符集合
private DataTable Ys
{
get {return (DataTable)ViewState["Ys"];}
set {ViewState["Ys"] = value;}
}
//Table表的行号
private int tid
{
get {return (int)(ViewState["tid"]);}
set {ViewState["tid"] = value;}
}
//由于最后一次添加的条件行没有被服务器接收,需要刷新一次
private bool ReceptChildControls
{
get {return (bool)(ViewState["RCC"]);}
set {ViewState["RCC"] = value;}
}
//保存字段类型
private string[] FieldType
{
get {return (string[])(ViewState["FieldType"]);}
set {ViewState["FieldType"] = value;}
}
protected override void Render(HtmlTextWriter output)
{
if (Site != null && Site.DesignMode)
CreateChildControls();

base.Render(output);
}
protected override void CreateChildControls()
{
Controls.Clear();
ClearChildViewState();
BuildControlHierarchy();
}
private void BuildControlHierarchy()
{
if(tid==0)
{
//第一次生成
string strSql;
int iPos;
string[] strTableNames;
strSql = BaseSqlCommand.ToLower();
strSql = strSql.Substring(strSql.IndexOf("from") + 5);
iPos = strSql.IndexOf("order by");
if(iPos > -1)
strSql = strSql.Substring(0, iPos);
iPos = strSql.IndexOf("group by");
if(iPos > -1)
strSql = strSql.Substring(0, iPos);
iPos = strSql.IndexOf("where");
if(iPos > -1)
strSql = strSql.Substring(0, iPos);
strSql = strSql.Trim();
strTableNames = strSql.Split(',');

string strWhere="";
foreach(string strTn in strTableNames)
{
if(strWhere=="")
strWhere=" AND (dbo.sysobjects.name = '"+strTn+"'";
else
strWhere+=" or dbo.sysobjects.name = '"+strTn+"'";

}
strWhere+=")";
DataTable data=new DataTable();
strSql="SELECT CASE dbo.systypes.name WHEN 'datetime' THEN 'Convert(char(10),' + rtrim(dbo.sysobjects.name) + '.' + ltrim(dbo.syscolumns.name) + ',120)' ELSE CASE dbo.systypes.name WHEN 'ntext' THEN 'convert(varchar,' + rtrim(dbo.sysobjects.name) + '.' + ltrim(dbo.syscolumns.name) + ')' ELSE CASE dbo.systypes.name WHEN 'text' THEN 'convert(varchar,' + rtrim(dbo.sysobjects.name) + '.' + ltrim(dbo.syscolumns.name) + ')' ELSE rtrim(dbo.sysobjects.name) + '.' + ltrim(dbo.syscolumns.name) END END END AS FieldName, " +
"RTRIM(CONVERT(char(20), dbo.sysproperties.[value])) AS ChinesName, dbo.systypes.name AS FieldType " +
"FROM dbo.syscolumns INNER JOIN dbo.systypes " +
"ON dbo.syscolumns.xtype = dbo.systypes.xtype INNER JOIN dbo.sysobjects " +
"ON dbo.syscolumns.id = dbo.sysobjects.id INNER JOIN dbo.sysproperties " +
"ON dbo.syscolumns.id = dbo.sysproperties.id AND dbo.syscolumns.colid = dbo.sysproperties.smallid " +
"WHERE (RTRIM(CONVERT(char(100), dbo.sysproperties.[value])) <> '') AND " +
"(LEN(RTRIM(CONVERT(char(20), dbo.sysobjects.id))) > 8)" + strWhere + " ORDER BY dbo.sysobjects.name, dbo.syscolumns.colid";
SqlDataAdapter adapter = new SqlDataAdapter(strSql, ConnectionString);
adapter.Fill(data);
adapter.Dispose();
Fn=data;
string[] ft=new string[data.Rows.Count];
for(int i=0;i<data.Rows.Count;i++)
{
ft[i]=data.Rows[i]["FieldType"].ToString();
}
FieldType=ft;
DataTable dtAs = new DataTable("ArithmeticSymbol");
dtAs.Columns.Add("cn", System.Type.GetType("System.String"));
dtAs.Columns.Add("en", System.Type.GetType("System.String"));
dtAs.Rows.Add(new string[]{"大于"," > "});
dtAs.Rows.Add(new string[]{"大于等于"," >= "});
dtAs.Rows.Add(new string[]{"等于"," = "});
dtAs.Rows.Add(new string[]{"小于等于"," =< "});
dtAs.Rows.Add(new string[]{"小于"," < "});
dtAs.Rows.Add(new string[]{"不等于"," <> "});
dtAs.Rows.Add(new string[]{"含有"," like ",});
dtAs.Rows.Add(new string[]{"不含有"," not like "});
Ys=dtAs;
DataTable dtLs = new DataTable("LogicSymbol");
dtLs.Columns.Add("cn", System.Type.GetType("System.String"));
dtLs.Columns.Add("en", System.Type.GetType("System.String"));
dtLs.Rows.Add(new string[]{"并且"," AND "});
dtLs.Rows.Add(new string[]{"或者"," OR "});
Ls=dtLs;

t.Font.Name = Font.Name;
t.Font.Size = Font.Size;
t.BorderStyle = BorderStyle;
t.BorderWidth = BorderWidth;
t.BorderColor = BorderColor;
t.Width = Width;
t.Height = Height;
t.BackColor = BackColor;
t.ForeColor = ForeColor;

TableRow row = new TableRow();
row.ID=tid.ToString();
t.Rows.Add(row);

TableCell cellField = new TableCell();
DropDownList ddlFn = new DropDownList();
ddlFn.Font.Name=Font.Name;
ddlFn.Font.Size=Font.Size;
ddlFn.ID="ddlFn"+tid.ToString()+"1";
ddlFn.DataSource=data.DefaultView;
ddlFn.DataTextField="ChinesName";
ddlFn.DataValueField="FieldName";
ddlFn.SelectedIndexChanged+=new EventHandler(ddlFn_SelectedIndexChanged);
ddlFn.AutoPostBack=true;
ddlFn.DataBind();
cellField.Controls.Add(ddlFn);
row.Cells.Add(cellField);

TableCell cellArithmeticSymbol = new TableCell();
DropDownList ddlAs = new DropDownList();
ddlAs.Font.Name=Font.Name;
ddlAs.Font.Size=Font.Size;
ddlAs.ID="ddlAs"+tid.ToString()+"2";
ddlAs.DataSource=dtAs.DefaultView;
ddlAs.DataTextField="cn";
ddlAs.DataValueField="en";
ddlAs.DataBind();
cellArithmeticSymbol.Controls.Add(ddlAs);
row.Cells.Add(cellArithmeticSymbol);

TableCell cellText = new TableCell();
TextBox txbValue = new TextBox();
txbValue.Font.Name=Font.Name;
txbValue.Font.Size=Font.Size;
txbValue.ID="txbV"+tid.ToString()+"3";
txbValue.Width=120;
cellText.Controls.Add(txbValue);
row.Cells.Add(cellText);

TableCell cellLogicSymbol = new TableCell();
cellLogicSymbol.ID="td"+tid.ToString()+"3";
row.Cells.Add(cellLogicSymbol);

DropDownList ddlLs = new DropDownList();
ddlLs.Font.Name=Font.Name;
ddlLs.Font.Size=Font.Size;
ddlLs.ID="ddlLs"+tid.ToString()+"4";
ddlLs.DataSource=dtLs.DefaultView;
ddlLs.DataTextField="cn";
ddlLs.DataValueField="en";
ddlLs.DataBind();

TableCell cellAdd = new TableCell();
Button btnAdd = new Button();
btnAdd.Font.Name=Font.Name;
btnAdd.Font.Size=Font.Size;
btnAdd.ID="btnAdd";
btnAdd.Click += new EventHandler(btnAdd_Click);
btnAdd.Text=" 添加\n\r条件项";
btnAdd.Width=(int)(Math.Ceiling(Font.Size.Unit.Value*4.5));
cellAdd.Controls.Add(btnAdd);
row.Cells.Add(cellAdd);

TableCell cellQueryMake = new TableCell();
Button btnQueryMake = new Button();
btnQueryMake.Font.Name=Font.Name;
btnQueryMake.Font.Size=Font.Size;
btnQueryMake.ID="btnQueryMake";
btnQueryMake.Click += new EventHandler(btnQueryMake_Click);
btnQueryMake.Text=" 产生\n\r查询";
btnQueryMake.Width=(int)(Math.Ceiling(Font.Size.Unit.Value*4.5));
cellQueryMake.Controls.Add(btnQueryMake);
row.Cells.Add(cellQueryMake);
data.Dispose();
Controls.Add(t);
tid+=1;
}
else
{
//屏幕刷新
t.Font.Name = Font.Name;
t.Font.Size = Font.Size;
t.BorderStyle = BorderStyle;
t.BorderWidth = BorderWidth;
t.BorderColor = BorderColor;
t.Width = Width;
t.Height = Height;
t.BackColor = BackColor;
t.ForeColor = ForeColor;
for(int i=0; i<tid;i++)
{
TableRow row = new TableRow();
row.ID=i.ToString();
t.Rows.Add(row);

TableCell cellField = new TableCell();
DropDownList ddlFn = new DropDownList();
ddlFn.Font.Name=Font.Name;
ddlFn.Font.Size=Font.Size;
ddlFn.ID="ddlFn"+i.ToString()+"1";
ddlFn.DataSource=Fn.DefaultView;
ddlFn.DataTextField="ChinesName";
ddlFn.DataValueField="FieldName";
ddlFn.SelectedIndexChanged+=new EventHandler(ddlFn_SelectedIndexChanged);
ddlFn.AutoPostBack=true;
ddlFn.DataBind();
cellField.Controls.Add(ddlFn);
row.Cells.Add(cellField);

TableCell cellArithmeticSymbol = new TableCell();
DropDownList ddlAs = new DropDownList();
ddlAs.Font.Name=Font.Name;
ddlAs.Font.Size=Font.Size;
ddlAs.ID="ddlAs"+i.ToString()+"2";
ddlAs.DataSource=Ys.DefaultView;
ddlAs.DataTextField="cn";
ddlAs.DataValueField="en";
ddlAs.DataBind();
cellArithmeticSymbol.Controls.Add(ddlAs);
row.Cells.Add(cellArithmeticSymbol);

TableCell cellText = new TableCell();
TextBox txbValue = new TextBox();
txbValue.Font.Name=Font.Name;
txbValue.Font.Size=Font.Size;
txbValue.ID="txbV"+i.ToString()+"3";
txbValue.Width=120;
cellText.Controls.Add(txbValue);
row.Cells.Add(cellText);

if(i<tid-1)
{
TableCell cellLogicSymbol = new TableCell();
DropDownList ddlLs = new DropDownList();
ddlLs.Font.Name=Font.Name;
ddlLs.Font.Size=Font.Size;
ddlLs.ID="ddlLs"+i.ToString()+"4";
ddlLs.DataSource=Ls.DefaultView;
ddlLs.DataTextField="cn";
ddlLs.DataValueField="en";
ddlLs.DataBind();
cellLogicSymbol.Controls.Add(ddlLs);
row.Cells.Add(cellLogicSymbol);
}
else
{
TableCell cellLogicSymbol = new TableCell();
row.Cells.Add(cellLogicSymbol);
}
if(i==0)
{
TableCell cellAdd = new TableCell();
Button btnAdd = new Button();
btnAdd.Font.Name=Font.Name;
btnAdd.Font.Size=Font.Size;
btnAdd.ID="btnAdd";
btnAdd.Click += new EventHandler(btnAdd_Click);
btnAdd.Text=" 添加\n\r条件项";
btnAdd.Width=(int)(Math.Ceiling(Font.Size.Unit.Value*4.5));
cellAdd.Controls.Add(btnAdd);
row.Cells.Add(cellAdd);

TableCell cellQueryMake = new TableCell();
btnQueryMake.ID="btnQueryMake";
btnQueryMake.Width=(int)(Math.Ceiling(Font.Size.Unit.Value*4.5));//自动调节宽度
btnQueryMake.Font.Name=Font.Name;
btnQueryMake.Font.Size=Font.Size;
if(ReceptChildControls||tid==1)
{
btnQueryMake.Click += new EventHandler(btnQueryMake_Click);
}
else
{
btnQueryMake.Click += new EventHandler(btnRecept_Click);
}
cellQueryMake.Controls.Add(btnQueryMake);
row.Cells.Add(cellQueryMake);
}
Controls.Add(t);
}
//产生一个空白行用于日期控件的安置
TableRow kbh = new TableRow();
TableCell blankline = new TableCell();
blankline.ColumnSpan=6;
Calendar cd=new Calendar();
cd.Font.Name=Font.Name;
cd.Font.Size=11;
cd.Visible=Convert.ToBoolean(ViewState["Calendar"]);
cd.SelectionChanged+=new EventHandler(cd_SelectionChanged);
cd.SelectedDate=System.DateTime.Today;
blankline.Controls.Add(cd);
kbh.Cells.Add(blankline);
t.Rows.Add(kbh);
Controls.Add(t);
}
}

private void ddlFn_SelectedIndexChanged(object sender, EventArgs e)
{
int Rs=Convert.ToInt16(((DropDownList)sender).ID.Substring(5,((DropDownList)sender).ID.Length-6));
if(FieldType[((DropDownList)sender).SelectedIndex].IndexOf("time")>-1)
{
((TextBox)(t.Rows[Rs].Cells[2].Controls[0])).Width=80;
((TextBox)(t.Rows[Rs].Cells[2].Controls[0])).ReadOnly=true;
HtmlAnchor ha = new HtmlAnchor();
ha.ID="hA"+Rs.ToString();
ha.ServerClick+=new EventHandler(ha_ServerClick);
HtmlImage img=new HtmlImage();
img.Src=imgUrl;
ha.Controls.Add(img);
t.Rows[Rs].Cells[2].Controls.Add(ha);
}
}
private void ha_ServerClick(object sender, EventArgs e)
{
ViewState["CurrentLine"]=Convert.ToInt16(((HtmlAnchor)sender).ID.Substring(2,((HtmlAnchor)sender).ID.Length-2));
((Calendar)(t.Rows[t.Rows.Count-1].Cells[0].Controls[0])).Visible=true;
ViewState["Calendar"]=true;
}
private void cd_SelectionChanged(object sender, EventArgs e)
{
((TextBox)(t.Rows[Convert.ToInt16(ViewState["CurrentLine"])].Cells[2].Controls[0])).Text=((Calendar)sender).SelectedDate.ToString("yyyy-MM-dd");
((Calendar)sender).Visible=false;
ViewState["Calendar"]=false;
}
private void btnRecept_Click(object sender, EventArgs e)
{
((Button)(sender)).Text=" 产生\n\r查询";
((Button)(sender)).Width=(int)(Math.Ceiling(Font.Size.Unit.Value*4.5));
ReceptChildControls=true;
}
private void btnQueryMake_Click(object sender, EventArgs e)
{
((Button)(sender)).Text=" 产生\n\r查询";
((Button)(sender)).Width=(int)(Math.Ceiling(Font.Size.Unit.Value*4.5));
string strSql="";
string strLastLogicSymbol="";
for(int i=0;i<t.Rows.Count-1;i++)
{
if(((TextBox)(t.Rows[i].Cells[2].Controls[0])).Text.Trim()!="")
{
switch(FieldType[((DropDownList)(t.Rows[i].Cells[0].Controls[0])).SelectedIndex])
{
//非数字类型
case "datetime":
case "smalldatetime":
case "timestamp":
case "bit":
case "char":
case "image":
case "nchar":
case "ntext":
case "nvarchar":
case "sql_variant":
case "sysname":
case "text":
case "uniqueidentifier":
case "varchar":
if(((DropDownList)(t.Rows[i].Cells[1].Controls[0])).SelectedValue.Trim()=="like" || ((DropDownList)(t.Rows[i].Cells[1].Controls[0])).SelectedValue.Trim()=="not like")
strSql+=((DropDownList)(t.Rows[i].Cells[0].Controls[0])).SelectedValue+" "+((DropDownList)(t.Rows[i].Cells[1].Controls[0])).SelectedValue+" '%"+((TextBox)(t.Rows[i].Cells[2].Controls[0])).Text.Trim()+"%' "+(i==t.Rows.Count-2?"":((DropDownList)(t.Rows[i].Cells[3].Controls[0])).SelectedValue);
else
strSql+=((DropDownList)(t.Rows[i].Cells[0].Controls[0])).SelectedValue+" "+((DropDownList)(t.Rows[i].Cells[1].Controls[0])).SelectedValue+" '"+((TextBox)(t.Rows[i].Cells[2].Controls[0])).Text.Trim()+"' "+(i==t.Rows.Count-2?"":((DropDownList)(t.Rows[i].Cells[3].Controls[0])).SelectedValue);
break;
default:
//数字类型
if(((DropDownList)(t.Rows[i].Cells[1].Controls[0])).SelectedValue.Trim()=="like" || ((DropDownList)(t.Rows[i].Cells[1].Controls[0])).SelectedValue.Trim()=="not like")
strSql+="Convert(varchar,"+((DropDownList)(t.Rows[i].Cells[0].Controls[0])).SelectedValue+") "+((DropDownList)(t.Rows[i].Cells[1].Controls[0])).SelectedValue+" '%"+((TextBox)(t.Rows[i].Cells[2].Controls[0])).Text.Trim()+"%' "+(i==t.Rows.Count-2?"":((DropDownList)(t.Rows[i].Cells[3].Controls[0])).SelectedValue);
else
strSql+=((DropDownList)(t.Rows[i].Cells[0].Controls[0])).SelectedValue+" "+((DropDownList)(t.Rows[i].Cells[1].Controls[0])).SelectedValue+" "+((TextBox)(t.Rows[i].Cells[2].Controls[0])).Text.Trim()+" "+(i==t.Rows.Count-2?"":((DropDownList)(t.Rows[i].Cells[3].Controls[0])).SelectedValue);
break;
}
strLastLogicSymbol=(i==t.Rows.Count-2?"":((DropDownList)(t.Rows[i].Cells[3].Controls[0])).SelectedValue.Trim());
}
}
switch(strLastLogicSymbol)
{
case "AND":
strSql+=" 1=1 ";
break;
case "OR":
strSql+=" 1=2 ";
break;
default:
break;
}
if(strSql.Length>0)
{
string strBaseSql=BaseSqlCommand.ToLower();
int iOPos=strBaseSql.IndexOf("order by");
int iGPos=strBaseSql.IndexOf("group by");
int iWPos=strBaseSql.IndexOf("where");
if(iWPos==-1 && iGPos==-1 && iOPos==-1)
FullSqlCommand=BaseSqlCommand+" WHERE "+strSql;
else if(iWPos==-1 && iGPos==-1 && iOPos>-1)
FullSqlCommand=strBaseSql.Substring(0,iOPos-1)+" WHERE "+strSql+strBaseSql.Substring(iOPos-1);
else if(iWPos==-1 && iGPos>-1)
FullSqlCommand=strBaseSql.Substring(0,iGPos-1)+" WHERE "+strSql+strBaseSql.Substring(iGPos-1);
else if(iWPos>-1)
FullSqlCommand=strBaseSql.Substring(0,iWPos-1)+" WHERE ("+strSql+") AND "+strBaseSql.Substring(iWPos+5);
}
else
{
FullSqlCommand=BaseSqlCommand;
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
DropDownList ddlLs = new DropDownList();
ddlLs.Font.Name=Font.Name;
ddlLs.Font.Size=Font.Size;
ddlLs.ID="ddlLs"+tid.ToString()+"4";
ddlLs.DataSource=Ls.DefaultView;
ddlLs.DataTextField="cn";
ddlLs.DataValueField="en";
ddlLs.DataBind();
t.Rows[tid-1].Cells[3].Controls.Add(ddlLs);

tid+=1;
TableRow row = new TableRow();
row.ID=tid.ToString();
t.Rows.Add(row);

TableCell cellField = new TableCell();
DropDownList ddlFn = new DropDownList();
ddlFn.Enabled=false;
ddlFn.Font.Name=Font.Name;
ddlFn.Font.Size=Font.Size;
ddlFn.ID="ddlFn"+tid.ToString()+"1";
ddlFn.DataSource=Fn.DefaultView;
ddlFn.DataTextField="ChinesName";
ddlFn.DataValueField="FieldName";
ddlFn.DataBind();
cellField.Controls.Add(ddlFn);
row.Cells.Add(cellField);

TableCell cellArithmeticSymbol = new TableCell();
DropDownList ddlAs = new DropDownList();
ddlAs.Enabled=false;
ddlAs.Font.Name=Font.Name;
ddlAs.Font.Size=Font.Size;
ddlAs.ID="ddlAs"+tid.ToString()+"2";
ddlAs.DataSource=Ys.DefaultView;
ddlAs.DataTextField="cn";
ddlAs.DataValueField="en";
ddlAs.DataBind();
cellArithmeticSymbol.Controls.Add(ddlAs);
row.Cells.Add(cellArithmeticSymbol);

TableCell cellText = new TableCell();
TextBox txbValue = new TextBox();
txbValue.Enabled=false;
txbValue.Font.Name=Font.Name;
txbValue.Font.Size=Font.Size;
txbValue.ID="txbV"+tid.ToString()+"3";
txbValue.Width=120;
cellText.Controls.Add(txbValue);
row.Cells.Add(cellText);
Controls.Add(t);
btnQueryMake.Text=" 刷新\n\r新添项";
ReceptChildControls=false;
}
}
}
posted on 2005-11-13 20:16  Mack.Z  阅读(306)  评论(0)    收藏  举报