热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

asp.net中的WebFrom

*******************说明***************************作者:清风携夕阳时间:2014-09-29描述:Web服务端控件辅助类,程序开发过程
,,
///******************* 说明 ***************************///
/// 作者:清风携夕阳
/// 时间:2014-09-29
/// 描述:Web服务端控件辅助类,程序开发过程中常用方法
///***************************************************///
using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace Common
{
/// 
/// Web服务端控件辅助类
/// 
[Serializable]
public static class WebHelper
{
#region 常量、静态变量
/// 
/// 8位时间格式yyyymmdd
/// 
public static string time8 = "yyyymmdd";
/// 
/// 10位时间格式yyyy-mm-dd
/// 
public static string time10 = "yyyy-mm-dd";
/// 
/// 通用空值选项文本
/// 
public static string emptySelect = "--请选择--";
#endregion
#region 验证、检测方法
/// 
/// 验证sql匹配条件是否正确(若以and开头则自动去除)
/// 
/// sql匹配条件
public static string CheckStrWhere(string strWhere)
{
string str = strWhere.TrimStart();//去除前置空格
if (str.ToLower().IndexOf("and ") == 0)//若以and开头则自动去除第一个and
{
strWhere = str.Substring(4);//若要保留前面一个空格,可以改为3
}
return strWhere;
}
#endregion
#region 服务端控件方法

#region CheckBoxList
/// 
/// 获取CheckBoxList选中项数目
/// 
public static int CheckedCount(CheckBoxList ckboxlist)
{
int count = 0;
foreach (ListItem item in ckboxlist.Items)
{
if (item.Selected == true)
{
count++;
}
}
return count;
}
/// 
/// 根据选项值选中CheckBoxList选项
/// 
public static void SetChecked(CheckBoxList cboxlist, List<string> vals)
{
if (vals == null || vals.Count == 0)
{
return;
}
for (int i = 0; i )
{
ListItem item = cboxlist.Items[i];
for (int j = 0; j )
{
if (item.Value == vals[j])
{
item.Selected = true;
vals.Remove(vals[j]);
break;
}
}
if (vals.Count == 0)
{
return;
}
}
}
/// 
/// 获取CheckBoxList选中项的值
/// 
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;
}
/// 
/// 清空选项
/// 
public static void ClearChecked(CheckBoxList cboxlist)
{
foreach (ListItem item in cboxlist.Items)
{
item.Selected = false;
}
}
/// 
/// 全选
/// 
public static void CheckAll(CheckBoxList cboxlist)
{
foreach (ListItem item in cboxlist.Items)
{
item.Selected = true;
}
}
/// 
/// 反选
/// 
public static void CheckNotChecked(CheckBoxList cboxlist)
{
foreach (ListItem item in cboxlist.Items)
{
item.Selected = !item.Selected;
}
}
/// 
/// 根据数据表绑定CheckBoxList控件
/// 
/// 数据表
/// 选项名称列编码
/// 选项值列编码
public static void BindCheckBoxList(CheckBoxList cboxlist, DataTable dt, string TextField, string ValueField)
{
cboxlist.Items.Clear();
if (dt != null && dt.Rows.Count > 0)
{
cboxlist.DataSource = dt;
cboxlist.DataTextField = TextField;
cboxlist.DataValueField = ValueField;
cboxlist.DataBind();
}
}
#endregion
#region RadioButtonList
/// 
/// 根据数据表绑定RadioButtonList控件
/// 
/// 数据
/// 选项名称列编码
/// 选项值列编码
public static void BindRadioButtonList(RadioButtonList rdolist, DataTable dt, string TextField, string ValueField)
{
rdolist.Items.Clear();
if (dt != null && dt.Rows.Count > 0)
{
rdolist.DataSource = dt;
rdolist.DataTextField = TextField;
rdolist.DataValueField = ValueField;
rdolist.DataBind();
}
}
#endregion
#region DropDownList
/// 
/// 根据数据表绑定RadioButtonList控件
/// 
/// 数据表
/// 选项名称列编码
/// 选项值列编码
/// 空值显示文本,若为空则无空值选项
public static void BindDropDownList(DropDownList dlist, DataTable dt, string TextField, string ValueField, string EmptyValueText)
{
dlist.Items.Clear();
if (dt != null && dt.Rows.Count > 0)
{
dlist.DataSource = dt;
dlist.DataTextField = TextField;
dlist.DataValueField = ValueField;
dlist.DataBind();
}
if (!String.IsNullOrEmpty(EmptyValueText))
{
dlist.Items.Insert(0, new ListItem(EmptyValueText, ""));
}
}
#endregion
#region ListBox
/// 
/// 根据数据表绑定ListBox控件
/// 
/// 数据表
/// 选项名称列编码
/// 选项值列编码
public static void BindListBox(ListBox lbox, DataTable dt, string TextField, string ValueField)
{
lbox.Items.Clear();
if (dt != null && dt.Rows.Count > 0)
{
lbox.DataSource = dt;
lbox.DataTextField = TextField;
lbox.DataValueField = ValueField;
lbox.DataBind();
}
}
/// 
/// 根据选项文本查找并选中ListBox选项
/// 
/// ListBox
/// 选项显示的文本
public static void FindAndFixItemByText(ListBox lbox, string strValue)
{
int count = lbox.Items.Count;
int index = lbox.SelectedIndex;
if (count > 0)
{
int i = index + 1;
for (; i )
{
ListItem li = lbox.Items[i];
if (li.Text.Contains(strValue))
{
lbox.SelectedIndex = i;
break;
}
if (index > 0 && i == count - 1)
{
count = index;
i = 0;
index = 0;
}
}
}
}
#endregion
#region TreeView 2013-08-12
/// 
/// 展开指定节点的所有上级节点
/// 
public static void ExpandAllParentNode(TreeNode tn)
{
if (tn.Parent != null)
{
tn.Parent.Expand();
ExpandAllParentNode(tn.Parent);
}
}
/// 
/// 清空TreeView节点选中状态
/// 
public static void ClearTreeNodesChecked(TreeView tview)
{
if (tview.Nodes.Count > 0)
{
foreach (TreeNode tn in tview.Nodes)
{
ClearTreeNodesChecked(tn);
}
}
}
/// 
/// 清空子节点选中状态
/// 
public static void ClearTreeNodesChecked(TreeNode tn)
{
if (tn != null)
{
tn.Checked = false;
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode child in tn.ChildNodes)
{
ClearTreeNodesChecked(child);
}
}
}
}
/// 
/// 根据节点Value值查找节点
/// 
/// 根节点
/// 节点值
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;
}
/// 
/// 根据节点Value值查找节点
/// 
/// TreeView
/// 节点值
public static TreeNode FindNodeByValue(TreeView tview, string strValue)
{
if (tview.Nodes.Count == 0)
return null;
TreeNode tnRet = null;
foreach (TreeNode tn in tview.Nodes)
{
tnRet = FindNodeByValue(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// 
/// 根据节点Value值查找指定层级的节点
/// 
/// 根节点
/// 节点层级
/// 节点值
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 //不去查找更深层次的节点
{
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNodeByValue(tn, depth, strValue);
if (tnRet != null) break;
}
}
return tnRet;
}
/// 
/// 根据节点Value值查找指定层级的节点
/// 
/// TreeView
/// 节点层级
/// 节点值
public static TreeNode FindNodeByValue(TreeView tview, int depth, string strValue)
{
if (tview.Nodes.Count == 0)
return null;
TreeNode tnRet = null;
foreach (TreeNode tn in tview.Nodes)
{
tnRet = FindNodeByValue(tn, depth, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// 
/// 根据节点显示名称查找节点
/// 
/// 根节点
/// 节点显示名称
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;
}
/// 
/// 根据节点显示名称查找节点
/// 
/// TreeView
/// 节点显示名称
public static TreeNode FindNodeByText(TreeView tview, string strValue)
{
if (tview.Nodes.Count == 0)
return null;
TreeNode tnRet = null;
foreach (TreeNode tn in tview.Nodes)
{
tnRet = FindNodeByText(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// 
/// 根据节点显示名称查找指定层级的节点
/// 
/// 根节点
/// 节点层级
/// 节点显示名称
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 //不去查找更深层级的节点
{
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNodeByText(tn, depth, strValue);
if (tnRet != null) break;
}
}
return tnRet;
}
/// 
/// 根据节点显示名称查找指定层级的节点
/// 
/// TreeView
/// 节点层级
/// 节点显示名称
public static TreeNode FindNodeByText(TreeView tview, int depth, string strValue)
{
if (tview.Nodes.Count == 0)
return null;
TreeNode tnRet = null;
foreach (TreeNode tn in tview.Nodes)
{
tnRet = FindNodeByText(tn, depth, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// 
/// 根据节点Value值选中指定层级的节点
/// 
/// 节点层级
/// 节点值
public static TreeNode CheckNodeByValue(TreeView tview, int depth, string strValue)
{
TreeNode tn = FindNodeByValue(tview, depth, strValue);
if (tn != null)
{
tn.Checked = true;
}
return tn;
}
/// 
/// 根据节点显示名称选中指定层级的节点
/// 
/// TreeView
/// 节点层级
/// 节点显示名称
public static TreeNode CheckNodeByText(TreeView tview, int depth, string strValue)
{
TreeNode tn = FindNodeByText(tview, depth, strValue);
if (tn != null)
{
tn.Checked = true;
}
return tn;
}
/// 
/// 根据节点Value值查找并选定节点
/// 
/// 节点值
public static TreeNode FixNodeByValue(TreeView tview, string strValue)
{
TreeNode tn = FindNodeByValue(tview, strValue);
if (tn != null)
{
ExpandAllParentNode(tn);
tn.Select();
}
return tn;
}
/// 
/// 根据节点显示名称查找并选定节点
/// 
/// TreeView
/// 节点显示名称
public static TreeNode FixNodeByText(TreeView tview, string strValue)
{
TreeNode tn = FindNodeByText(tview, strValue);
if (tn != null)
{
ExpandAllParentNode(tn);
tn.Select();
}
return tn;
}
/// 
/// 展开第一序列节点并选中最底层节点
/// 
/// 根节点
/// tview
public static void ExpandFirstsNode(TreeNode root, TreeView tview)
{
if (root.ChildNodes.Count > 0)
{
ExpandFirstsNode(root.ChildNodes[0], tview);
}
else
{
root.Select();
}
}
/// 
/// 展开第一序列节点并选中最底层节点
/// 
public static void ExpandFirstsNode(TreeView tview)
{
if (tview.Nodes.Count > 0)
{
ExpandFirstsNode(tview.Nodes[0], tview);
}
}
#endregion

#endregion
#region html控件方法

#region select
/// 
/// 获取下拉选项htm
/// 
/// 数据集
/// 选项值字段
/// 选项文本字段
/// 空值文本,若为空则无空值选项
public static string GetSelectOptionHtm(DataTable dt, string valueField, string textField, string emptyText)
{
string htm = String.Empty;
if (!String.IsNullOrEmpty(emptyText))
{
htm += "" + emptyText + "\r\n";
}
if (dt != null)
{
for (int i = 0; i )
{
htm += "\r\n";
}
}
return htm;
}
/// 
/// 绑定下拉列表(runat=‘server‘的select)
/// 
/// 数据集
/// 选项值字段
/// 选项文本字段
/// 空值文本,若为空则无空值选项
public static void BindSelectList(DataTable dt,HtmlSelect select,string valueField,string textField,string emptyText)
{
select.Items.Clear();
if (dt != null && dt.Rows.Count > 0)
{
select.DataSource = dt;
select.DataValueField = valueField;
select.DataTextField = textField;
select.DataBind();
}
if (!String.IsNullOrEmpty(emptyText))
{
select.Items.Insert(0, new System.Web.UI.WebControls.ListItem(emptyText, ""));
}
}
#endregion

#endregion
}
}
B/S项目常用方法

asp.net中的WebFrom


推荐阅读
  • vector:在vc6中,如果要镶嵌使用vector,如vector,后面的两个应该用,空格隔开,否则被编译器认为是移位符string::npos的值为 ... [详细]
  • 一、在androidStudio中实现tabs比较简单,新建项目就可以选择tabs模板进行创建,默认实现tabs功能:直接运行项目就可以看到效果:可以说非常简单,但是我们在实际开发 ... [详细]
  • Adapter相当于C(Controller,控制器),listView相当于V(View,视图)用于显示数据为ListView提供数据的List,数组或数据库相当于MVC模式中的 ... [详细]
  • 摘自:https:www.cnblogs.comnick-huangp4076273.htmlselect*from(select'Nick'asitemfromd ... [详细]
  • Spark 贝叶斯分类算法
    一、贝叶斯定理数学基础我们都知道条件概率的数学公式形式为即B发生的条件下A发生的概率等于A和B同时发生的概率除以B发生的概率。根据此公式变换,得到贝叶斯公式:即贝叶斯定律是关于随机 ... [详细]
  • 安全3AAuthentication:认证Authorzation:授权Accouting|Audition:审计用户管理用户:UID:0,不一定是root,root的uid非0时 ... [详细]
  • 【实践】基于RTThread的智慧路灯案例实验分享
    之前分享了基于LiteOS的智慧农业案例实验分享基于LiteOS的智慧农业案例实验分享,阅读量挺不错,看样子大家都挺喜欢这种实验。那咱们就再来一个类似的实验:基于RT-Thread ... [详细]
  • ASP.NET Core WebAPI 开发新建WebAPI项目  转
    转 http:www.cnblogs.comlinezerop5497472.htmlASP.NETCoreWebAPI开发-新建WebAPI项目ASP.NETCoreWebAPI ... [详细]
  • 步骤一:明确主打的核心目标用户群(对应产品侧的定位)这个核心目标用户群体是该产品成功挤进市场的切入点,甚至是撬动市场的支点和撬杠。市面上几乎很少有产品是专门给一个群体用而对其他群体 ... [详细]
  • UDP协议开发
    UDP是用户数据报协议(UserDatagramProtocol,UDP)的简称,其主要作用是将网络数据流量压缩成数据报形式,提供面向事务的简单信息传送服务。与TCP协议不同,UD ... [详细]
  • 1.数据准备#测试数组vectorc(5,34,65,36,67,3,6,43,69,59,25,785,10,11,14)vector##[1]53465366736436959 ... [详细]
  • webpack 配置IP 和端口号
    最近在用webpack搭建本地服务器的时候,因为不想总是用localhost来跑,所以对webpack.config.js进行了配置,如下devServer:{publicPath ... [详细]
  • #includestdafx.h#includeiostream#includesstream#includemap#includestring ... [详细]
  • 题目:Givenanintegerarray,youneedtofindone continuoussubarray thatifyouonlysortthissubarrayin ... [详细]
  • CentOS7.2详细安装步骤(二)
    7)语言设置(可以在上一个主界面进行设置,这里不用再次设置)8)SECURITY设置(安全设置)选择default(默认的)策略就可以,通过进行选择,单击完成即可Default#默 ... [详细]
author-avatar
刘诗宪668964
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有