using System;
using System.Data;// DataSet,DataTable,DataView
using System.Configuration;// ConfigurationManager
using Oracle.DataAccess.Client;// oracle 连接类
using System.Web.UI.WebControls;// DropDownList,GridView,TreeNodeCollection,TreeView,TreeNode
using System.Collections;// ArrayList
/// <summary>
/// DrawWidget 的摘要说明
/// </summary>
public class DrawWidget
{
/// <summary>
/// 构造函数
/// </summary>
public DrawWidget() { }
/// <summary>
/// 绑定列表框
/// </summary>
/// <param name="listBox">列表框对象</param>
/// <param name="list">列表框内容</param>
/// <returns>无</returns>
public void BindListBox(ListBox listBox, ArrayList list)
{
listBox.DataSource = list;
listBox.DataBind();
}
/// <summary>
/// 添加列表框项
/// </summary>
/// <param name="listBox">列表框对象</param>
/// <param name="strText">添加选项的文本</param>
/// <returns>无</returns>
public void AddListBox(ListBox listBox, string strText)
{
listBox.Items.Add(strText);
}
/// <summary>
/// 删除列表框项
/// </summary>
/// <param name="listBox">列表框对象</param>
/// <param name="index">要删除的选项索引号</param>
/// <returns>无</returns>
public void DelListBox(ListBox listBox, int index)
{
listBox.Items.RemoveAt(index);
}
/// <summary>
/// 统计列表框
/// </summary>
/// <param name="listBox">列表框对象</param>
/// <returns>统计列表框项数值</returns>
public int CountListBox(ListBox listBox)
{
return listBox.Items.Count;
}
/// <summary>
/// 重置列表框
/// </summary>
/// <param name="listBox">列表框对象</param>
/// <param name="isEnabled">重置后是否可用</param>
/// <returns>无</returns>
public void ResetListBox(ListBox listBox, bool isEnabled)
{
listBox.Items.Clear();
listBox.Enabled = isEnabled;
}
/// <summary>
/// 获取列表框选中数值
/// </summary>
/// <param name="listBox">列表框对象</param>
/// <returns>选中的选项数值</returns>
public string GetListBoxValue(ListBox listBox)
{
return listBox.SelectedValue;
}
/// <summary>
/// 获取列表框选中文本
/// </summary>
/// <param name="listBox">列表框对象</param>
/// <returns>选中的选项文本</returns>
public string GetListBoxText(ListBox listBox)
{
return listBox.SelectedItem.Text;
}
/// <summary>
/// 获取当前选中列表框索引
/// </summary>
/// <param name="listBox">列表框对象</param>
/// <returns>选中的选项索引号</returns>
public int GetSelectedListBox(ListBox listBox)
{
return listBox.SelectedIndex;
}
/// <summary>
/// 设定当前选中列表框索引
/// </summary>
/// <param name="listBox">列表框对象</param>
/// <param name="index">指定的选项索引号</param>
/// <returns>无</returns>
public void SetSelectedListBox(ListBox listBox, int index)
{
listBox.SelectedIndex = index;
}
/// <summary>
/// 绑定下拉列表
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <param name="list">下拉列表内容</param>
/// <returns>无</returns>
public void BindDropList(DropDownList dropDownList, ArrayList list)
{
dropDownList.DataSource = list;
dropDownList.DataBind();
}
/// <summary>
/// 添加下拉列表项
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <param name="strText">添加选项的文本</param>
/// <param name="strValue">添加选项的数值</param>
/// <returns>无</returns>
public void AddDropList(DropDownList dropDownList, string strText, string strValue)
{
dropDownList.Items.Add(new ListItem(strText, strValue));
}
/// <summary>
/// 添加下拉列表项
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <param name="orderNum">指定要添加选项的位置</param>
/// <param name="strText">添加选项的文本</param>
/// <param name="strValue">添加选项的数值</param>
/// <returns>无</returns>
public void AddDropList(DropDownList dropDownList, int orderNum, string strText, string strValue)
{
dropDownList.Items.Insert(orderNum, new ListItem(strText, strValue));
}
/// <summary>
/// 删除下拉列表项
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <param name="index">要删除的选项索引号</param>
/// <returns>无</returns>
public void DelDropList(DropDownList dropDownList, int index)
{
dropDownList.Items.RemoveAt(index);
}
/// <summary>
/// 统计下拉列表
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <returns>统计下拉列表项数值</returns>
public int CountDropList(DropDownList dropDownList)
{
return dropDownList.Items.Count;
}
/// <summary>
/// 重置下拉列表
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <param name="isEnabled">重置后是否可用</param>
/// <returns>无</returns>
public void ResetDropList(DropDownList dropDownList, bool isEnabled)
{
dropDownList.Items.Clear();
dropDownList.Enabled = isEnabled;
}
/// <summary>
/// 获取下拉列表选中数值
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <returns>选中的选项数值</returns>
public string GetDropListValue(DropDownList dropDownList)
{
return dropDownList.SelectedValue;
}
/// <summary>
/// 获取下拉列表选中文本
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <returns>选中的选项文本</returns>
public string GetDropListText(DropDownList dropDownList)
{
return dropDownList.SelectedItem.Text;
}
/// <summary>
/// 获取当前选中下拉列表索引
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <returns>选中的选项索引号</returns>
public int GetSelectedDropList(DropDownList dropDownList)
{
return dropDownList.SelectedIndex;
}
/// <summary>
/// 设定当前选中下拉列表索引
/// </summary>
/// <param name="dropDownList">下拉列表对象</param>
/// <param name="index">指定的选项索引号</param>
/// <returns>无</returns>
public void SetSelectedDropList(DropDownList dropDownList, int index)
{
dropDownList.SelectedIndex = index;
}
/// <summary>
/// 绑定网格视图
/// </summary>
/// <param name="gridView">网格控件对象</param>
/// <param name="dataTable">要绑定的数据表</param>
/// <returns>无</returns>
public void BindGridView(GridView gridView, DataTable dataTable)
{
try
{
gridView.DataSource = dataTable;
gridView.DataBind();
// 设定行列
gridView.RowStyle.Height = 30;
gridView.HeaderRow.Height = 40;
for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
gridView.Rows[i].Cells[j].Wrap = false;
}
}
}
catch
{
}
}
/// <summary>
/// 绑定网格视图
/// </summary>
/// <param name="gridView">网格控件对象</param>
/// <param name="dataTable">要绑定的数据表</param>
/// <param name="title">标题名</param>
/// <param name="headerList">列名表</param>
/// <returns>无</returns>
public void BindGridView(GridView gridView, DataTable dataTable, string title, ArrayList headerList)
{
try
{
gridView.DataSource = dataTable;
gridView.DataBind();
// 设定标题
gridView.Caption = title;
// 设定行列
gridView.RowStyle.Height = 30;
gridView.HeaderRow.Height = 40;
for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
gridView.Rows[i].Cells[j].Wrap = false;
}
}
// 设定列名
for (int i = 0; i < headerList.Count; i++)
{
try
{
gridView.HeaderRow.Cells[i].Text = headerList[i].ToString();
gridView.HeaderRow.Cells[i].Wrap = false;
}
catch
{
break;
}
}
}
catch
{
}
}
/// <summary>
/// 获取指定名称的节点对象
/// </summary>
/// <param name="menuNodes">菜单节点集合</param>
/// <param name="nodeText">要查找的节点文本</param>
/// <returns>找到的节点对象</returns>
/// <example>
/// DrawWidget.getNodeByName(treeView1.Nodes, "完井数据表");
/// </example>
public TreeNode GetNodeByName(TreeNodeCollection menuNodes, string nodeText)
{
TreeNode result = null;
foreach (TreeNode node in menuNodes)
{
if (node.Text == nodeText)
{
result = node;
}
}
return result;
}
/// <summary>
/// 添加节点
/// </summary>
/// <param name="menuNodes">菜单节点集合</param>
/// <param name="menuValue">节点编号</param>
/// <param name="menuText">节点文本</param>
/// <param name="menuImgUrl">图标地址</param>
/// <param name="menuNaviUrl">链接地址</param>
/// <param name="menuTarget">指向目标区</param>
/// <returns>无</returns>
/// <example>
/// DrawWidget.addNode(treeView1.Nodes, "a", "test", null, "test.aspx", null);
/// </example>
public void AddNode(TreeNodeCollection menuNodes, string menuValue, string menuText, string menuImgUrl, string menuNaviUrl, string menuTarget)
{
TreeNode newNode = new TreeNode();
newNode.Value = menuValue;
newNode.Text = menuText;
newNode.ImageUrl = menuImgUrl;
newNode.NavigateUrl = menuNaviUrl;
newNode.Target = menuTarget;
newNode.SelectAction = TreeNodeSelectAction.SelectExpand;// 选择后展开
menuNodes.Add(newNode);
}
/// <summary>
/// 添加子节点
/// </summary>
/// <param name="menuNode">菜单节点对象</param>
/// <param name="menuValue">节点编号</param>
/// <param name="menuText">节点文本</param>
/// <param name="menuImgUrl">图标地址</param>
/// <param name="menuNaviUrl">链接地址</param>
/// <param name="menuTarget">指向目标区</param>
/// <returns>无</returns>
/// <example>
/// DrawWidget.addChildNode(DrawWidget.getNodeByName(treeView1.Nodes, "完井数据表"), "A1", "a1", null, null, null);
/// </example>
public void AddChildNode(TreeNode menuNode, string menuValue, string menuText, string menuImgUrl, string menuNaviUrl, string menuTarget)
{
TreeNode newNode = new TreeNode();
newNode.Value = menuValue;
newNode.Text = menuText;
newNode.ImageUrl = menuImgUrl;
newNode.NavigateUrl = menuNaviUrl;
newNode.Target = menuTarget;
newNode.SelectAction = TreeNodeSelectAction.SelectExpand;// 选择后展开
menuNode.ChildNodes.Add(newNode);
}
/// <summary>
/// 递归生成菜单树
/// </summary>
/// <param name="menuTree">菜单树对象</param>
/// <param name="menuTable">递归菜单数据表</param>
/// <param name="menuRootId">起始根菜单编号</param>
/// <param name="pNode">当前递归节点</param>
/// <param name="f_menuId">节点编号</param>
/// <param name="f_parentId">父节点编号</param>
/// <param name="f_menuName">节点文本</param>
/// <param name="f_ImgUrl">图标地址</param>
/// <param name="f_NaviUrl">链接地址</param>
/// <param name="menuTarget">指向目标区</param>
/// <returns>无</returns>
/// <example>
/// DrawWidget.addTree(treeView1, OracleDatabase.getDataTable("select * from menus"), "root", null, "MENU_ID", "PARENT_ID", "MENU_NAME", "IMG_URL", "NAVI_URL", null);
/// </example>
public void AddTree(TreeView menuTree, DataTable menuTable, string menuRootId, TreeNode pNode, string f_menuId, string f_parentId, string f_menuName, string f_ImgUrl, string f_NaviUrl, string menuTarget)
{
try
{
DataView dvTree = new DataView(menuTable);
if (pNode == null)
{// 根节点过滤
dvTree.RowFilter = "[" + f_menuId + "]='" + menuRootId + "'";
}
else
{// 子节点过滤
dvTree.RowFilter = "[" + f_parentId + "]='" + menuRootId + "'";
}
foreach (DataRowView dvRow in dvTree)
{
TreeNode newNode = new TreeNode();
newNode.Value = dvRow["" + f_menuId + ""].ToString();
newNode.Text = dvRow["" + f_menuName + ""].ToString();
newNode.ImageUrl = dvRow[f_ImgUrl].ToString().Length > 0 ? dvRow[f_ImgUrl].ToString() : null;
newNode.NavigateUrl = dvRow[f_NaviUrl].ToString().Length > 0 ? dvRow[f_NaviUrl].ToString() : null;
newNode.Target = menuTarget;// 指向目标区
if (newNode.NavigateUrl.Length == 0)
{// 地址为空时
newNode.SelectAction = TreeNodeSelectAction.Expand;// 点击后展开
}
if (pNode == null)
{// 添加当前节点的根节点
menuTree.Nodes.Add(newNode);
}
else
{// 添加当前节点的子节点
newNode.Expanded = false;// 子节点默认展开模式
pNode.ChildNodes.Add(newNode);
}
AddTree(menuTree, menuTable, dvRow["" + f_menuId + ""].ToString(), newNode, f_menuId, f_parentId, f_menuName, f_ImgUrl, f_NaviUrl, menuTarget);// 递归调用
}
}
catch
{
}
}
/// <summary>
/// 清空菜单树
/// </summary>
/// <param name="menuTree">菜单树对象</param>
/// <returns>无</returns>
public void ResetTree(TreeView menuTree)
{
menuTree.Nodes.Clear();
}
/// <summary>
/// 绘制表
/// </summary>
/// <param name="connStr">连接字符串</param>
/// <param name="tabName">查询表名</param>
/// <param name="html">输出绘制结果超文本</param>
/// <returns>无</returns>
public void DrawTable(string connStr, string tabName, out string html)
{
DataTable table = new DataTable();
ArrayList list = new ArrayList();
tabName = tabName.ToUpper();//表名转大写
/* 设置命令参数 */
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings[connStr].ConnectionString);
OracleCommand cmd = conn.CreateCommand();
conn.Open();// 打开连接
cmd.InitialLONGFetchSize = -1;// 设定为抓取不限长度的Long字段
/* 读表数据 */
cmd.CommandText = "select * from " + tabName;
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(table);
/* 读表备注名 */
cmd.CommandText = "select comments from user_tab_comments where table_name='" + tabName + "'";
OracleDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
table.TableName = dr[0].ToString();
}
/* 读列备注名 */
cmd.CommandText = "select comments from user_col_comments where table_name='" + tabName + "'";
dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(dr[0].ToString());// 每次读取后数据前推,因此只需读首行即可
}
dr.Close();// 关闭读取器
conn.Close();// 关闭连接
/* 绘制表格 */
html = "<table border=\"1\" style=\"text-align:center;vertical-align:middle;border:2px solid darkgray;border-collapse:collapse;width:100%;\">";
// 表名
html += "<tr><td colspan=\"" + table.Columns.Count + "\"><h2>" + table.TableName + "</h2></td></tr>";
// 表头
html += "<tr>";
for (int i = 0; i < list.Count; i++)
{
html += "<td><h3>" + list[i].ToString() + "</h3></td>";
}
html += "</tr>";
// 表值
for (int row = 0; row < table.Rows.Count; row++)
{
html += "<tr>";
for (int col = 0; col < table.Columns.Count; col++)
{
html += "<td>" + table.Rows[row][col].ToString() + "</td>";
}
html += "</tr>";
}
html += "</table>";
}
/// <summary>
/// 绘制表
/// </summary>
/// <param name="connStr">连接字符串</param>
/// <param name="tabName">查询表名</param>
/// <param name="whereClause">查询子句</param>
/// <param name="title">显示标题</param>
/// <returns>返回绘制结果</returns>
public string DrawTable(string connStr, string tabName, string whereClause, string title)
{
string html = null;
DataTable table = new DataTable();
ArrayList list = new ArrayList();
tabName = tabName.ToUpper();//表名转大写
/* 设置命令参数 */
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings[connStr].ConnectionString);
OracleCommand cmd = conn.CreateCommand();
conn.Open();// 打开连接
cmd.InitialLONGFetchSize = -1;// 设定为抓取不限长度的Long字段
/* 读表数据 */
cmd.CommandText = "select * from " + tabName + " " + whereClause;
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(table);
/* 读表备注名 */
table.TableName = title;
/* 读列备注名 */
cmd.CommandText = "select comments from user_col_comments where table_name='" + tabName + "'";
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(dr[0].ToString());// 每次读取后数据前推,因此只需读首行即可
}
dr.Close();// 关闭读取器
conn.Close();// 关闭连接
/* 绘制表格 */
html = "<table border=\"1\" style=\"text-align:center;vertical-align:middle;border:2px solid darkgray;border-collapse:collapse;width:100%;\">";
// 表名
html += "<tr><td colspan=\"" + table.Columns.Count + "\"><h2>" + table.TableName + "</h2></td></tr>";
// 表头
html += "<tr>";
for (int i = 0; i < list.Count; i++)
{
html += "<td><h3>" + list[i].ToString() + "</h3></td>";
}
html += "</tr>";
// 表值
for (int row = 0; row < table.Rows.Count; row++)
{
html += "<tr>";
for (int col = 0; col < table.Columns.Count; col++)
{
html += "<td>" + table.Rows[row][col].ToString() + "</td>";
}
html += "</tr>";
}
html += "</table>";
return html;
}
/// <summary>
/// 绘制表
/// </summary>
/// <param name="table">要绘制的数据表</param>
/// <param name="title">显示的标题</param>
/// <param name="field">字段名列表</param>
/// <returns>返回绘制结果</returns>
public string DrawTable(DataTable table, string title, ArrayList field)
{
string html = null;
/* 设置表名 */
table.TableName = title;
/* 绘制表格 */
html = "<table border=\"1\" style=\"text-align:center;vertical-align:middle;border:2px solid darkgray;border-collapse:collapse;width:100%;\">";
// 表名
html += "<tr><td colspan=\"" + table.Columns.Count + "\"><h2>" + table.TableName + "</h2></td></tr>";
// 表头
html += "<tr>";
if (field != null)
{
for (int i = 0; i < field.Count; i++)
{
html += "<td><h3>" + field[i].ToString() + "</h3></td>";
}
}
else
{
for (int i = 0; i < table.Columns.Count; i++)
{
html += "<td><h3>" + table.Columns[i].Caption + "</h3></td>";
}
}
html += "</tr>";
// 表值
foreach (DataRow row in table.Rows) // Loop over the rows.
{
html += "<tr>";
foreach (var item in row.ItemArray) // Loop over the items.
{
html += "<td>" + item + "</td>";
}
html += "</tr>";
}
html += "</table>";
return html;
}
/// <summary>
/// 表过滤
/// </summary>
/// <param name="table">要过滤的数据表</param>
/// <param name="field_name">要定位的字段名</param>
/// <param name="search_item">要查询的项内容</param>
/// <returns>返回过滤后的表</returns>
public DataTable TableFilter(DataTable table, string field_name, string search_item)
{
DataView dv = new DataView(table);
dv.RowFilter = "[" + field_name.ToUpper() + "]='" + search_item + "'";
return dv.ToTable();
}
}