当前位置:首页>>ASP.NET教程>>Asp.NET综合技巧>>.net中一些所封装的类
.net中一些所封装的类
2009/11/8 13:19:18
using System;
using System.IO;
using System.Web;
using System.Data;

using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using Microsoft.Web.UI.WebControls;
using System.Drawing;
using System.Text;
using OWC;

namespace JSL.MVC.Page
{
/// <summary>
/// Controls 的摘要说明。
/// </summary>
public class Controls
{
private Controls()
{
}
#region string
public static string Encrypto(string Source)
{
System.Security.Cryptography.HashAlgorithm HashCryptoService;
HashCryptoService = new System.Security.Cryptography.SHA1Managed();

byte[] bytIn = System.Text.UTF8Encoding.UTF8.GetBytes(Source);
byte[] bytOut = HashCryptoService.ComputeHash(bytIn);
return Convert.ToBase64String(bytOut);
}
#endregion

#region BaseDataList
public void BindBaseList(System.Web.UI.WebControls.BaseDataList bdl_object,DataSet ds, string tbName)
{
bdl_object.DataSource=ds.Tables[tbName];
bdl_object.DataBind();
}

public void BindBaseList(System.Web.UI.WebControls.BaseDataList bdl_object,DataTable dt)
{
bdl_object.DataSource=dt;
bdl_object.DataBind();
}
#endregion

#region DropDownList
public static void CreateDropDownList(DropDownList ddl_object, DataTable dt, string s_text,string s_value)
{
ddl_object.DataSource = dt;
ddl_object.DataTextField = s_text;
ddl_object.DataValueField = s_value;
ddl_object.DataBind();
}

public static void CreateDropDownList(DropDownList ddl_object, DataSet ds, string s_text,string s_value)
{
ddl_object.DataSource = ds;
ddl_object.DataTextField = s_text;
ddl_object.DataValueField = s_value;
ddl_object.DataBind();
}

public static void CreateDropDownList(DropDownList ddl_object, string s_text, string s_value)
{
ListItem li = new ListItem(s_text, s_value);
ddl_object.Items.Add(li);
}

public static void CreateDropDownList(DropDownList ddl_object, string[] s_text, string[] s_value)
{
for (int i = 0; i < s_text.Length; i++)
{
ListItem li = new ListItem(s_text[i],s_value[i]);
ddl_object.Items.Add(li);
}
}

public static void CopyDropDownList(DropDownList ddl_source, DropDownList ddl_target)
{
ddl_target.Items.Clear();
ddl_target.DataTextField = ddl_source.DataTextField;
ddl_target.DataTextFormatString = ddl_source.DataTextFormatString;
ddl_target.DataValueField = ddl_source.DataValueField;
for (int i = 0; i < ddl_source.Items.Count; i++)
{
ddl_target.Items.Add(ddl_source.Items[i]);
}
}

public static void SetDropDownListValue(DropDownList ddl_object, string s_value)
{
foreach(ListItem li in ddl_object.Items)
{
if (li.Value == s_value)
{
ddl_object.SelectedValue = s_value;
break;
}
else
{
ddl_object.SelectedValue = null;
}
}
}

public static void SetDropDownListValue(DropDownList[] ddl_object,string[] s_value)
{
int i = 0;
foreach(DropDownList ddl in ddl_object)
{
SetDropDownListValue(ddl,s_value[i++]);
}
}
#endregion

#region TextBox
protected static void CopyTextBox(TextBox tb_source, TextBox tb_target)
{
tb_target.Text = tb_source.Text;
}

protected static void SetTextBoxValue(TextBox tb_object, string s_value)
{
tb_object.Text = s_value;
}

protected static void SetTextBoxValue(TextBox[] tb_object,string[] s_value)
{
int i = 0;
foreach(TextBox dt in tb_object)
{
SetTextBoxValue(dt,s_value[i++]);
}
}
#endregion

#region TreeView
/// <summary>
/// 创建树形结构
/// </summary>
/// <param name="dt">要传入的数据表</param>
/// <param name="treenodecollection">树结点容器集合</param>
/// <param name="MapNodeData">结点主键</param>
/// <param name="MapText">显示内容</param>
/// <param name="MapNavigateURL">导航路径</param>
/// <param name="ImageURL">图片地址</param>
/// <param name="ParentName">结点父键</param>
/// <param name="RelationOperation">结点过滤关系运算符</param>
/// <param name="ParentValue">运算比较值</param>
/// <param name="ParentValueType">比较值类型</param>
/// <param name="SortExpression">排序方式</param>
/// <param name="bcheckbox">选择框显示与否</param>
/// <param name="MapStatus">选择框的内容</param>
/// <param name="MapTarget">用于树保存时的分层标记</param>
public static void CreateViewTree(DataTable dt, TreeNodeCollection treenodecollection,
string MapNodeData, string MapText, string MapNavigateURL, string ParentName,
string RelationOperation, string ParentValue, string ParentValueType,string SortExpression,
bool bcheckbox, string MapStatus, string MapTarget)
{
if (ParentValueType == "'") ParentValue = ParentValueType + ParentValue + ParentValueType;

string str = ParentName + RelationOperation + ParentValue;
DataRow[] drs = dt.Select( ParentName + RelationOperation + ParentValue, SortExpression);
foreach(DataRow dr in drs)
{
TreeNode treenode = new TreeNode();
treenode.Text = dr[MapText].ToString();
try
{
treenode.NavigateUrl = dr[MapNavigateURL].ToString();
}
catch{treenode.NavigateUrl = "";}
treenode.CheckBox = bcheckbox;
if (MapTarget != "" && MapTarget != null) treenode.TreeNodeXsltSrc = dr[MapTarget].ToString();
if (treenode.CheckBox)
{
bool bcheck ;
try
{
bcheck = (dr[MapStatus].ToString() == "1");
}
catch{bcheck = false;}
treenode.Checked =bcheck ;
}
string treenodenodedata = treenode.NodeData = dr[MapNodeData].ToString();
treenodecollection.Add(treenode);
if (ParentValueType == "'") treenodenodedata =ParentValueType+ treenodenodedata + ParentValueType;

DataRow[] tmpdrs = dt.Select(ParentName + RelationOperation + treenodenodedata, SortExpression);
if ( tmpdrs.Length > 0)
{
CreateViewTree(dt,treenode.Nodes,MapNodeData,MapText,MapNavigateURL,ParentName,RelationOperation,treenode.NodeData,ParentValueType,SortExpression,bcheckbox,MapStatus,MapTarget);
}
}
}
#endregion

#region Session
public static bool CheckSession(string CheckSession,System.Web.UI.Page WebPage)
{
bool b=false;
foreach(string sTmp in WebPage.Session.Keys)
{
if (sTmp.Equals(CheckSession))
{
b = true;
break;
}
}
return b;
}

public static void CheckSession(string CheckSession,System.Web.UI.Page WebPage,string Erro