BS常用方法备忘
在B/S项目开发过程中总结的一些常用方法,如:常量、验证方法、服务器控件方法、html控件方法等。
///******************* 说明 ***************************///
/// 作者:清风携夕阳
/// 时间:2014-09-29
/// 描述:Web服务端控件辅助类,程序开发过程中常用方法
///***************************************************///
using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace Common
{
/// <summary>
/// Web服务端控件辅助类
/// </summary>
[Serializable]
public static class WebHelper
{
#region 常量、静态变量
/// <summary>
/// 8位时间格式yyyymmdd
/// </summary>
public static string time8 = "yyyymmdd";
/// <summary>
/// 10位时间格式yyyy-mm-dd
/// </summary>
public static string time10 = "yyyy-mm-dd";
/// <summary>
/// 通用空值选项文本
/// </summary>
public static string emptySelect = "--请选择--";
#endregion
#region 验证、检测方法
/// <summary>
/// 验证sql匹配条件是否正确(若以and开头则自动去除)
/// </summary>
/// <param name="strWhere">sql匹配条件</param>
public static string CheckStrWhere(string strWhere)
{
string str = strWhere.TrimStart();//去除前置空格
if (str.ToLower().IndexOf("and ") == )//若以and开头则自动去除第一个and
{
strWhere = str.Substring();//若要保留前面一个空格,可以改为3
}
return strWhere;
}
#endregion
#region 服务端控件方法 #region CheckBoxList
/// <summary>
/// 获取CheckBoxList选中项数目
/// </summary>
public static int CheckedCount(CheckBoxList ckboxlist)
{
int count = ;
foreach (ListItem item in ckboxlist.Items)
{
if (item.Selected == true)
{
count++;
}
}
return count;
}
/// <summary>
/// 根据选项值选中CheckBoxList选项
/// </summary>
public static void SetChecked(CheckBoxList cboxlist, List<string> vals)
{
if (vals == null || vals.Count == )
{
return;
}
for (int i = ; i < cboxlist.Items.Count; i++)
{
ListItem item = cboxlist.Items[i];
for (int j = ; j < vals.Count; j++)
{
if (item.Value == vals[j])
{
item.Selected = true;
vals.Remove(vals[j]);
break;
}
}
if (vals.Count == )
{
return;
}
}
}
/// <summary>
/// 获取CheckBoxList选中项的值
/// </summary>
public static List<string> GetChecked(CheckBoxList cboxlist)
{
List<string> vals = new List<string>();
foreach (ListItem item in cboxlist.Items)
{
if (item.Selected == true)
{
vals.Add(item.Value);
}
}
return vals;
}
/// <summary>
/// 清空选项
/// </summary>
public static void ClearChecked(CheckBoxList cboxlist)
{
foreach (ListItem item in cboxlist.Items)
{
item.Selected = false;
}
}
/// <summary>
/// 全选
/// </summary>
public static void CheckAll(CheckBoxList cboxlist)
{
foreach (ListItem item in cboxlist.Items)
{
item.Selected = true;
}
}
/// <summary>
/// 反选
/// </summary>
public static void CheckNotChecked(CheckBoxList cboxlist)
{
foreach (ListItem item in cboxlist.Items)
{
item.Selected = !item.Selected;
}
}
/// <summary>
/// 根据数据表绑定CheckBoxList控件
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="TextField">选项名称列编码</param>
/// <param name="ValueField">选项值列编码</param>
public static void BindCheckBoxList(CheckBoxList cboxlist, DataTable dt, string TextField, string ValueField)
{
cboxlist.Items.Clear();
if (dt != null && dt.Rows.Count > )
{
cboxlist.DataSource = dt;
cboxlist.DataTextField = TextField;
cboxlist.DataValueField = ValueField;
cboxlist.DataBind();
}
}
#endregion
#region RadioButtonList
/// <summary>
/// 根据数据表绑定RadioButtonList控件
/// </summary>
/// <param name="dt">数据</param>
/// <param name="TextField">选项名称列编码</param>
/// <param name="ValueField">选项值列编码</param>
public static void BindRadioButtonList(RadioButtonList rdolist, DataTable dt, string TextField, string ValueField)
{
rdolist.Items.Clear();
if (dt != null && dt.Rows.Count > )
{
rdolist.DataSource = dt;
rdolist.DataTextField = TextField;
rdolist.DataValueField = ValueField;
rdolist.DataBind();
}
}
#endregion
#region DropDownList
/// <summary>
/// 根据数据表绑定RadioButtonList控件
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="TextField">选项名称列编码</param>
/// <param name="ValueField">选项值列编码</param>
/// <param name="ListName">空值显示文本,若为空则无空值选项</param>
public static void BindDropDownList(DropDownList dlist, DataTable dt, string TextField, string ValueField, string EmptyValueText)
{
dlist.Items.Clear();
if (dt != null && dt.Rows.Count > )
{
dlist.DataSource = dt;
dlist.DataTextField = TextField;
dlist.DataValueField = ValueField;
dlist.DataBind();
}
if (!String.IsNullOrEmpty(EmptyValueText))
{
dlist.Items.Insert(, new ListItem(EmptyValueText, ""));
}
}
#endregion
#region ListBox
/// <summary>
/// 根据数据表绑定ListBox控件
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="TextField">选项名称列编码</param>
/// <param name="ValueField">选项值列编码</param>
public static void BindListBox(ListBox lbox, DataTable dt, string TextField, string ValueField)
{
lbox.Items.Clear();
if (dt != null && dt.Rows.Count > )
{
lbox.DataSource = dt;
lbox.DataTextField = TextField;
lbox.DataValueField = ValueField;
lbox.DataBind();
}
}
/// <summary>
/// 根据选项文本查找并选中ListBox选项
/// </summary>
/// <param name="lbox">ListBox</param>
/// <param name="strValue">选项显示的文本</param>
public static void FindAndFixItemByText(ListBox lbox, string strValue)
{
int count = lbox.Items.Count;
int index = lbox.SelectedIndex;
if (count > )
{
int i = index + ;
for (; i < count; i++)
{
ListItem li = lbox.Items[i];
if (li.Text.Contains(strValue))
{
lbox.SelectedIndex = i;
break;
}
if (index > && i == count - )
{
count = index;
i = ;
index = ;
}
}
}
}
#endregion
#region TreeView 2013-08-12
/// <summary>
/// 展开指定节点的所有上级节点
/// </summary>
public static void ExpandAllParentNode(TreeNode tn)
{
if (tn.Parent != null)
{
tn.Parent.Expand();
ExpandAllParentNode(tn.Parent);
}
}
/// <summary>
/// 清空TreeView节点选中状态
/// </summary>
public static void ClearTreeNodesChecked(TreeView tview)
{
if (tview.Nodes.Count > )
{
foreach (TreeNode tn in tview.Nodes)
{
ClearTreeNodesChecked(tn);
}
}
}
/// <summary>
/// 清空子节点选中状态
/// </summary>
public static void ClearTreeNodesChecked(TreeNode tn)
{
if (tn != null)
{
tn.Checked = false;
if (tn.ChildNodes.Count > )
{
foreach (TreeNode child in tn.ChildNodes)
{
ClearTreeNodesChecked(child);
}
}
}
}
/// <summary>
/// 根据节点Value值查找节点
/// </summary>
/// <param name="tnParent">根节点</param>
/// <param name="strValue">节点值</param>
public static TreeNode FindNodeByValue(TreeNode tnParent, string strValue)
{
if (tnParent == null)
return null;
if (tnParent.Value == strValue)
return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNodeByValue(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// <summary>
/// 根据节点Value值查找节点
/// </summary>
/// <param name="tview">TreeView</param>
/// <param name="strValue">节点值</param>
public static TreeNode FindNodeByValue(TreeView tview, string strValue)
{
if (tview.Nodes.Count == )
return null;
TreeNode tnRet = null;
foreach (TreeNode tn in tview.Nodes)
{
tnRet = FindNodeByValue(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// <summary>
/// 根据节点Value值查找指定层级的节点
/// </summary>
/// <param name="tnParent">根节点</param>
/// <param name="depth">节点层级</param>
/// <param name="strValue">节点值</param>
public static TreeNode FindNodeByValue(TreeNode tnParent, int depth, string strValue)
{
if (tnParent == null)
return null;
if (tnParent.Value == strValue && tnParent.Depth == depth)
return tnParent;
TreeNode tnRet = null;
if (tnParent.Depth < depth)//不去查找更深层次的节点
{
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNodeByValue(tn, depth, strValue);
if (tnRet != null) break;
}
}
return tnRet;
}
/// <summary>
/// 根据节点Value值查找指定层级的节点
/// </summary>
/// <param name="tview">TreeView</param>
/// <param name="depth">节点层级</param>
/// <param name="strValue">节点值</param>
public static TreeNode FindNodeByValue(TreeView tview, int depth, string strValue)
{
if (tview.Nodes.Count == )
return null;
TreeNode tnRet = null;
foreach (TreeNode tn in tview.Nodes)
{
tnRet = FindNodeByValue(tn, depth, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// <summary>
/// 根据节点显示名称查找节点
/// </summary>
/// <param name="tnParent">根节点</param>
/// <param name="strValue">节点显示名称</param>
public static TreeNode FindNodeByText(TreeNode tnParent, string strValue)
{
if (tnParent == null)
return null;
if (tnParent.Text == strValue)
return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNodeByText(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// <summary>
/// 根据节点显示名称查找节点
/// </summary>
/// <param name="tview">TreeView</param>
/// <param name="strValue">节点显示名称</param>
public static TreeNode FindNodeByText(TreeView tview, string strValue)
{
if (tview.Nodes.Count == )
return null;
TreeNode tnRet = null;
foreach (TreeNode tn in tview.Nodes)
{
tnRet = FindNodeByText(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// <summary>
/// 根据节点显示名称查找指定层级的节点
/// </summary>
/// <param name="tnParent">根节点</param>
/// <param name="depth">节点层级</param>
/// <param name="strValue">节点显示名称</param>
public static TreeNode FindNodeByText(TreeNode tnParent, int depth, string strValue)
{
if (tnParent == null)
return null;
if (tnParent.Text == strValue && tnParent.Depth == depth)
return tnParent;
TreeNode tnRet = null;
if (tnParent.Depth < depth)//不去查找更深层级的节点
{
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNodeByText(tn, depth, strValue);
if (tnRet != null) break;
}
}
return tnRet;
}
/// <summary>
/// 根据节点显示名称查找指定层级的节点
/// </summary>
/// <param name="tview">TreeView</param>
/// <param name="depth">节点层级</param>
/// <param name="strValue">节点显示名称</param>
public static TreeNode FindNodeByText(TreeView tview, int depth, string strValue)
{
if (tview.Nodes.Count == )
return null;
TreeNode tnRet = null;
foreach (TreeNode tn in tview.Nodes)
{
tnRet = FindNodeByText(tn, depth, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// <summary>
/// 根据节点Value值选中指定层级的节点
/// </summary>
/// <param name="depth">节点层级</param>
/// <param name="strValue">节点值</param>
public static TreeNode CheckNodeByValue(TreeView tview, int depth, string strValue)
{
TreeNode tn = FindNodeByValue(tview, depth, strValue);
if (tn != null)
{
tn.Checked = true;
}
return tn;
}
/// <summary>
/// 根据节点显示名称选中指定层级的节点
/// </summary>
/// <param name="tview">TreeView</param>
/// <param name="depth">节点层级</param>
/// <param name="strValue">节点显示名称</param>
public static TreeNode CheckNodeByText(TreeView tview, int depth, string strValue)
{
TreeNode tn = FindNodeByText(tview, depth, strValue);
if (tn != null)
{
tn.Checked = true;
}
return tn;
}
/// <summary>
/// 根据节点Value值查找并选定节点
/// </summary>
/// <param name="strValue">节点值</param>
public static TreeNode FixNodeByValue(TreeView tview, string strValue)
{
TreeNode tn = FindNodeByValue(tview, strValue);
if (tn != null)
{
ExpandAllParentNode(tn);
tn.Select();
}
return tn;
}
/// <summary>
/// 根据节点显示名称查找并选定节点
/// </summary>
/// <param name="tview">TreeView</param>
/// <param name="strValue">节点显示名称</param>
public static TreeNode FixNodeByText(TreeView tview, string strValue)
{
TreeNode tn = FindNodeByText(tview, strValue);
if (tn != null)
{
ExpandAllParentNode(tn);
tn.Select();
}
return tn;
}
/// <summary>
/// 展开第一序列节点并选中最底层节点
/// </summary>
/// <param name="root">根节点</param>
/// <param name="tview">tview</param>
public static void ExpandFirstsNode(TreeNode root, TreeView tview)
{
if (root.ChildNodes.Count > )
{
ExpandFirstsNode(root.ChildNodes[], tview);
}
else
{
root.Select();
}
}
/// <summary>
/// 展开第一序列节点并选中最底层节点
/// </summary>
public static void ExpandFirstsNode(TreeView tview)
{
if (tview.Nodes.Count > )
{
ExpandFirstsNode(tview.Nodes[], tview);
}
}
#endregion #endregion
#region html控件方法 #region select
/// <summary>
/// 获取下拉选项htm
/// </summary>
/// <param name="dt">数据集</param>
/// <param name="valueField">选项值字段</param>
/// <param name="textField">选项文本字段</param>
/// <param name="emptyText">空值文本,若为空则无空值选项</param>
public static string GetSelectOptionHtm(DataTable dt, string valueField, string textField, string emptyText)
{
string htm = String.Empty;
if (!String.IsNullOrEmpty(emptyText))
{
htm += "<option value=\"\">" + emptyText + "</option>\r\n";
}
if (dt != null)
{
for (int i = ; i < dt.Rows.Count; i++)
{
htm += "<option value=\"" + dt.Rows[i][valueField] + "\">" + dt.Rows[i][textField] + "</option>\r\n";
}
}
return htm;
}
/// <summary>
/// 绑定下拉列表(runat='server'的select)
/// </summary>
/// <param name="dt">数据集</param>
/// <param name="valueField">选项值字段</param>
/// <param name="textField">选项文本字段</param>
/// <param name="emptyText">空值文本,若为空则无空值选项</param>
public static void BindSelectList(DataTable dt,HtmlSelect select,string valueField,string textField,string emptyText)
{
select.Items.Clear();
if (dt != null && dt.Rows.Count > )
{
select.DataSource = dt;
select.DataValueField = valueField;
select.DataTextField = textField;
select.DataBind();
}
if (!String.IsNullOrEmpty(emptyText))
{
select.Items.Insert(, new System.Web.UI.WebControls.ListItem(emptyText, ""));
}
}
#endregion #endregion
}
}
BS常用方法备忘的更多相关文章
- JQuery中常用方法备忘
本文转载自博客园,原文地址 http://www.cnblogs.com/xzf158/archive/2008/10/14/logan.html 1.Window.onload 的JQuery方法 ...
- Vi命令备忘
备忘 Ctrl+u:向文件首翻半屏: Ctrl+d:向文件尾翻半屏: Ctrl+f:向文件尾翻一屏: Ctrl+b:向文件首翻一屏: Esc:从编辑模式切换到命令模式: ZZ:命令模式下保存当前文件所 ...
- Scroll view 备忘
Stroyboard中使用ScrollView 当我们使用Storyboard开发项目时,如果要往控制器上拖入一个ScrollView并且添加约束设置滚动区域,是有特殊的规定的: 拖入一个scroll ...
- PostgreSQL 速查、备忘手册 | PostgreSQL Quick Find and Tutorial
PostgreSQL 速查.备忘手册 作者:汪嘉霖 这是一个你可能需要的一个备忘手册,此手册方便你快速查询到你需要的常见功能.有时也有一些曾经被使用过的高级功能.如无特殊说明,此手册仅适用于 Linu ...
- GIS部分理论知识备忘随笔
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...
- python序列,字典备忘
初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...
- ExtJs4常用配置方法备忘
viewport布局常用属性 new Ext.Viewport({ layout: "border", renderTo: Ext.getBody(), defaults: { b ...
- [备忘] Automatically reset Windows Update components
这两天遇到Windows 10的更新问题,官方有一个小工具,可以用来修复Windows Update的问题,备忘如下 https://support.microsoft.com/en-us/kb/97 ...
- ECMAScript 5(ES5)中bind方法简介备忘
一直以来对和this有关的东西模糊不清,譬如call.apply等等.这次看到一个和bind有关的笔试题,故记此文以备忘. bind和call以及apply一样,都是可以改变上下文的this指向的.不 ...
随机推荐
- apache 开启压缩功能
apache如何开启压缩功能. 1,首先先确认是安装deflatte模块.如果未安装,可以重新编译apache添加参数--enable-deflate=shared ,或者扩展安装deflate模块, ...
- 将json格式日期(毫秒数)转成日常日期格式和日常格式时间对比
第一:是把生成的Json格式的时间转换,注意要看清楚时间的格式 function (cellval) { var date = new Date(parseInt(cellval.replace(&q ...
- HttpApplication事件执行顺序(转)
HttpApplication 类的实例(Global继承自该类)是在 ASP.NET 基础结构中创建的,而不是由用户直接创建的.HttpApplication 类的一个实例在其生存期内被用于处理多个 ...
- C#常用的内置委托
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- sizeof 和 strlen
1. sizeof 1.1 sizeof是一个独立的运算符,不是函数.sizeof给我们提供有关数据项目所分配的内存的大小.例如: 1 2 cout << sizeof(long) < ...
- html ul li 显示数据库
方法1: function insert() { var str=""; var data="你的数据库数据"; str="<ul>< ...
- Java IO之序列化
序列化机制是Java语言内建的一种对象持久化方式,可以很容易的在JVM中的活动对象和字节数组之间转换.它的一个重要用途就是远程方法调用的时候,用来对开发人员屏蔽底层实现细节(远端的开发人员不知道这个对 ...
- mysql导出数据到excel表中
mysql> select b.username,a.subject,a.money,FROM_UNIXTIME(a.ctime) from shop_pay a INNER JOIN ...
- 屏蔽鼠标右键功能JS
<script language="Javascript"> function hiderightKey(){ return false; } docum ...
- delphi 修改代码补全的快捷键(由Ctrl+Space 改为 Ctrl + alt + Space)(通过修改OpenTool生效)
delphi 的IDE快捷键与输入法切换键中突,以往的解决方法是下载一个ImeTool修改 windows 系统的快捷键 在 xp win7 都好使,但在win 10经常是修改完后,重启又失效了. 本 ...