zoukankan
html css js c++ java
一个Page页面多个CheckBoxList的客户端验证(.net1.1及2.0实现)
一,基于.net1.1
1、类文件
using
System;
using
System.Web.UI;
using
System.Web;
using
System.Web.UI.WebControls;
using
System.Text;
namespace
AgrExibition.Common.Framework.Util
...
{
欢迎与邀月交流,net技术与软件架构
2007/07/26
#region
downmoon 2007/07/26
/**/
///
<summary>
///
一个Page页面多个CheckBoxList的客户端验证(.net1.1实现)(downmoon)
///
</summary>
#endregion
public
class
RadioButtonListRequiredFieldValidator : BaseValidator
...
{
protected
override
bool
ControlPropertiesValid()
...
{
return
true
;
}
protected
override
bool
EvaluateIsValid()
...
{
return
this
.EvaluateIsChecked();
}
protected
bool
EvaluateIsChecked()
...
{
ListControl _listcontrol
=
((ListControl)
this
.FindControl(
this
.ControlToValidate));
foreach
( ListItem li
in
_listcontrol.Items )
...
{
if
(li.Selected)
return
true
;
}
return
false
;
}
protected
override
void
OnPreRender( EventArgs e )
...
{
if
(
this
.EnableClientScript )
...
{
this
.ClientScript(); }
base
.OnPreRender( e );
}
protected
void
ClientScript()
...
{
this
.Attributes[
"
evaluationfunction
"
]
=
"
cb_vefify
"
;
StringBuilder sb_Script
=
new
StringBuilder();
sb_Script.Append(
"
<script language="javascript">
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
function cb_vefify(val) {
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
var val =document.getElementById(val.controltovalidate);
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
var col = val.all;
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
if ( col != null ) {
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
for ( i = 0; i < col.length; i++ ) {
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
if (col.item(i).tagName.toUpperCase() == "INPUT") {
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
if ( col.item(i).checked ) {
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
return true;
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
}
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
}
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
}
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
return false;
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
}
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
}
"
);
sb_Script.Append(
"
"
);
sb_Script.Append(
"
</script>
"
);
if
(
!
this
.Page.IsClientScriptBlockRegistered(
"
RBLScript
"
))
...
{
this
.Page.RegisterClientScriptBlock(
"
RBLScript
"
, sb_Script.ToString() );
}
}
}
}
2、页面
<%
...
@ Register TagPrefix
=
"
AgrExibition
"
Namespace
=
"
AgrExibition.Common.Framework.Util
"
Assembly
=
"
AgrExibition.Common.Framework
"
%>
<
asp:CheckBoxList
id
="cbB_Property"
runat
="server"
RepeatDirection
="Horizontal"
RepeatColumns
="3"
>
<
asp:ListItem
Value
="1"
>
投资商
</
asp:ListItem
>
<
asp:ListItem
Value
="2"
>
投资促进机构
</
asp:ListItem
>
<
asp:ListItem
Value
="3"
>
代理商
</
asp:ListItem
>
<
asp:ListItem
Value
="4"
>
科研及信息单位协会
</
asp:ListItem
>
<
asp:ListItem
Value
="5"
>
零售商
</
asp:ListItem
>
<
asp:ListItem
Value
="6"
>
批发商
</
asp:ListItem
>
<
asp:ListItem
Value
="7"
>
仓储业
</
asp:ListItem
>
<
asp:ListItem
Value
="8"
>
跨国公司
</
asp:ListItem
>
<
asp:ListItem
Value
="9"
>
进出口商
</
asp:ListItem
>
<
asp:ListItem
Value
="10"
>
生产商
</
asp:ListItem
>
<
asp:ListItem
Value
="11"
>
超市
</
asp:ListItem
>
<
asp:ListItem
Value
="12"
>
酒店
</
asp:ListItem
>
<
asp:ListItem
Value
="13"
>
宾馆及后勤服务
</
asp:ListItem
>
<
asp:ListItem
Value
="-1"
>
其他
</
asp:ListItem
>
</
asp:CheckBoxList
>
<
asp:TextBox
id
="txtB_Property"
Runat
="Server"
CssClass
="input2"
Width
="400px"
/>
<
AgrExibition:RADIOBUTTONLISTREQUIREDFIELDVALIDATOR
id
="rbValid1"
Display
="Dynamic"
ErrorMessage
="请选择或输入业务性质!"
runat
="server"
ControlToValidate
="cbB_Property"
Text
="*"
>
*
</
AgrExibition:RADIOBUTTONLISTREQUIREDFIELDVALIDATOR
>
<
br
/>
<
asp:CheckBoxList
id
="cbPurpose"
runat
="server"
RepeatDirection
="Horizontal"
RepeatColumns
="3"
>
<
asp:ListItem
Value
="1"
>
采购产品
</
asp:ListItem
>
<
asp:ListItem
Value
="2"
>
搜集市场信息
</
asp:ListItem
>
<
asp:ListItem
Value
="3"
>
与业务伙伴会面
</
asp:ListItem
>
<
asp:ListItem
Value
="4"
>
了解行业动向
</
asp:ListItem
>
<
asp:ListItem
Value
="5"
>
观看观摩
</
asp:ListItem
>
<
asp:ListItem
Value
="6"
>
投资项目洽谈
</
asp:ListItem
>
<
asp:ListItem
Value
="7"
>
获取行业信息
</
asp:ListItem
>
<
asp:ListItem
Value
="8"
>
寻找供应商或买家
</
asp:ListItem
>
<
asp:ListItem
Value
="-1"
>
其他
</
asp:ListItem
>
</
asp:CheckBoxList
>
<
asp:TextBox
id
="txtPurpose"
Runat
="Server"
CssClass
="input2"
Width
="400px"
/>
<
AgrExibition:RADIOBUTTONLISTREQUIREDFIELDVALIDATOR
id
="rbValid2"
Display
="Dynamic"
ErrorMessage
="请选择或输入参会目的!"
runat
="server"
ControlToValidate
="cbPurpose"
Text
="*"
>
*
</
AgrExibition:RADIOBUTTONLISTREQUIREDFIELDVALIDATOR
>
<
asp:ValidationSummary
id
="ValidationSummary1"
runat
="server"
ShowMessageBox
="True"
ShowSummary
="False"
HeaderText
="请检查输入表单内容:"
></
asp:ValidationSummary
>
二、基于.net 2.0
1、类文件
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Text;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
ShiXun.Commoon.Web.Validor
...
{
欢迎与邀月交流,net技术与软件架构
2007/07/26
#region
downmoon 2007/07/26
/**/
///
<summary>
///
一个Page页面多个CheckBoxList的客户端验证(.net2.0实现)(downmoon)
///
</summary>
#endregion
[DefaultProperty(
"
ErrorMessage
"
), ToolboxData(
"
<{0}:ListControlRequiredFieldValidator runat=server></{0}:ListControlRequiredFieldValidator>
"
)]
public
class
ListControlRequiredFieldValidator : BaseValidator
...
{
/**/
///
<summary>
///
Validator Requirement
///
</summary>
///
<returns>
True if dependencies are valid.
</returns>
protected
override
bool
ControlPropertiesValid()
...
{
Control controlToValidate
=
FindControl(ControlToValidate)
as
ListControl;
return
(controlToValidate
!=
null
);
}
/**/
///
<summary>
///
Validator Requirement
///
</summary>
///
<returns>
true if ControlToValidate
///
has one item or more selected
</returns>
protected
override
bool
EvaluateIsValid()
...
{
return
this
.EvaluateIsChecked();
}
/**/
///
<summary>
///
Return true if an item in the list is selected.
///
</summary>
///
<returns>
true if ControlToValidate
///
has one item or more selected
</returns>
protected
bool
EvaluateIsChecked()
...
{
ListControl listToValidate
=
((ListControl)
this
.FindControl(
this
.ControlToValidate));
foreach
(ListItem li
in
listToValidate.Items)
...
{
if
(li.Selected
==
true
)
return
true
;
}
return
false
;
}
/**/
///
<summary>
///
Pre Render
///
</summary>
///
<param name="e"></param >
protected
override
void
OnPreRender(EventArgs e)
...
{
System.Web.HttpContext.Current.Trace.Write(
"
Override OnPreRender
"
);
if
(
this
.DetermineRenderUplevel()
&&
this
.EnableClientScript)
...
{
Page.ClientScript.RegisterExpandoAttribute(
this
.ClientID,
"
evaluationfunction
"
,
"
ListItemVerify
"
);
Page.ClientScript.RegisterExpandoAttribute(
this
.ClientID,
"
minimumNumberOfSelectedCheckBoxes
"
,
"
1
"
);
//
System.Web.UI.Page.(this.ClientID, "evaluationfunction", "ListItemVerify");
//
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "minimumNumberOfSelectedCheckBoxes", "1");
//
TODO: imporove to allow variable number.
this
.RegisterClientScript();
}
else
...
{
this
.Attributes.Remove(
"
evaluationfunction
"
);
}
base
.OnPreRender(e);
}
/**/
///
<summary>
///
Register the client script.
///
</summary>
protected
void
RegisterClientScript()
...
{
string
script
=
@"
<script language=""javascript"">
function ListItemVerify(val)
{
var control = document.getElementById(val.controltovalidate);
var minimumNumberOfSelectedCheckBoxes = parseInt(val.minimumNumberOfSelectedCheckBoxes);
var selectedItemCount = 0;
var liIndex = 0;
var currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
while (currentListItem != null)
{
if (currentListItem.checked) selectedItemCount++;
liIndex++;
currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
}
return selectedItemCount >= minimumNumberOfSelectedCheckBoxes;
}
</script>
"
;
this
.Page.ClientScript.RegisterClientScriptBlock(
typeof
(ListControlRequiredFieldValidator),
"
ListRequiredValidator_Script
"
, script);
}
}
}
2、页面
<%
...
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
<%
...
@ Register TagPrefix
=
"
ShiXun
"
NameSpace
=
"
ShiXun.Commoon.Web.Validor
"
Assembly
=
"
ShiXun.Commoon.Web.Validor
"
%>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
asp:CheckBoxList
id
="cbB_Property"
runat
="server"
RepeatDirection
="Horizontal"
RepeatColumns
="3"
>
<
asp:ListItem
Value
="1"
>
投资商
</
asp:ListItem
>
<
asp:ListItem
Value
="2"
>
投资促进机构
</
asp:ListItem
>
<
asp:ListItem
Value
="3"
>
代理商
</
asp:ListItem
>
<
asp:ListItem
Value
="4"
>
科研及信息单位协会
</
asp:ListItem
>
<
asp:ListItem
Value
="5"
>
零售商
</
asp:ListItem
>
<
asp:ListItem
Value
="6"
>
批发商
</
asp:ListItem
>
<
asp:ListItem
Value
="7"
>
仓储业
</
asp:ListItem
>
<
asp:ListItem
Value
="8"
>
跨国公司
</
asp:ListItem
>
<
asp:ListItem
Value
="9"
>
进出口商
</
asp:ListItem
>
<
asp:ListItem
Value
="10"
>
生产商
</
asp:ListItem
>
<
asp:ListItem
Value
="11"
>
超市
</
asp:ListItem
>
<
asp:ListItem
Value
="12"
>
酒店
</
asp:ListItem
>
<
asp:ListItem
Value
="13"
>
宾馆及后勤服务
</
asp:ListItem
>
<
asp:ListItem
Value
="-1"
>
其他
</
asp:ListItem
>
</
asp:CheckBoxList
>
<
ShiXun:ListControlRequiredFieldValidator
ControlToValidate
="cbB_Property"
display
="Dynamic"
ErrorMessage
="至少选择一项!"
EnableClientScript
="true"
runat
="Server"
>
至少选择一项
</
ShiXun:ListControlRequiredFieldValidator
>
<
br
/>
<
br
/>
<
asp:CheckBoxList
ID
="cbPurpose"
runat
="server"
RepeatColumns
="3"
RepeatDirection
="Horizontal"
>
<
asp:ListItem
Value
="1"
>
采购产品
</
asp:ListItem
>
<
asp:ListItem
Value
="2"
>
搜集市场信息
</
asp:ListItem
>
<
asp:ListItem
Value
="3"
>
与业务伙伴会面
</
asp:ListItem
>
<
asp:ListItem
Value
="4"
>
了解行业动向
</
asp:ListItem
>
<
asp:ListItem
Value
="5"
>
观看观摩
</
asp:ListItem
>
<
asp:ListItem
Value
="6"
>
投资项目洽谈
</
asp:ListItem
>
<
asp:ListItem
Value
="7"
>
获取行业信息
</
asp:ListItem
>
<
asp:ListItem
Value
="8"
>
寻找供应商或买家
</
asp:ListItem
>
<
asp:ListItem
Value
="-1"
>
其他
</
asp:ListItem
>
</
asp:CheckBoxList
>
<
ShiXun:ListControlRequiredFieldValidator
ControlToValidate
="cbPurpose"
display
="Dynamic"
ErrorMessage
="至少选择一项!"
EnableClientScript
="true"
runat
="Server"
>
至少选择一项
</
ShiXun:ListControlRequiredFieldValidator
>
<
br
/>
<
asp:CheckBoxList
ID
="cbChannel"
runat
="server"
RepeatColumns
="3"
RepeatDirection
="Horizontal"
>
<
asp:ListItem
Value
="1"
>
报纸等平面媒体
</
asp:ListItem
>
<
asp:ListItem
Value
="2"
>
网络
</
asp:ListItem
>
<
asp:ListItem
Value
="3"
>
电视
</
asp:ListItem
>
<
asp:ListItem
Value
="4"
>
朋友介绍
</
asp:ListItem
>
<
asp:ListItem
Value
="5"
>
客户推荐
</
asp:ListItem
>
<
asp:ListItem
Value
="-1"
>
其他
</
asp:ListItem
>
</
asp:CheckBoxList
>
<
ShiXun:ListControlRequiredFieldValidator
ControlToValidate
="cbChannel"
display
="Dynamic"
ErrorMessage
="至少选择一项!"
EnableClientScript
="true"
runat
="Server"
>
至少选择一项
</
ShiXun:ListControlRequiredFieldValidator
>
<
asp:TextBox
ID
="txtSourceChannel"
runat
="Server"
CssClass
="input2"
Width
="400px"
></
asp:TextBox
>
<
br
/>
<
br
/>
<
br
/>
<
asp:Button
ID
="Button1"
runat
="server"
Text
="Button"
/>
</
form
>
</
body
>
</
html
>
邀月注:本文版权由邀月和博客园共同所有,转载请注明出处。
助人等于自助!
3w@live.cn
查看全文
相关阅读:
Zookeeper基本使用(转)
mongon命令(转)
openstack之cinder
raw格式转换成qcow2格式
calico网络
route命令使用
guestfish修改镜像内容
基于etcd插件的CoreDNS动态域名添加
dns记录类型(转)
C语言 格式化输出--%m.n
原文地址:https://www.cnblogs.com/downmoon/p/1020089.html
最新文章
Hbase脚本小结
org.apache.hadoop.hbase.DoNotRetryIOException: Class org.apache.phoenix.coprocessor.MetaDataEndpointImpl cannot be loaded Set hbase.table.sanity.checks to false at conf or table descriptor if you want
RDD缓存学习
scala flatMap reduceLeft foldLeft
scala Wordcount
spark mysql读写
scala 测试类
spark源码 hashpartitioner
hive 中与mysql 中函数同名不同意的方法记录
深入学习c++--左值引用和右值引用
热门文章
深入学习c++--重新审视auto
深入学习c++(虚函数遇到析构函数就退化了)
Python高级笔记(五)--实例方法、静态方法和类方法
Python高级笔记(四) -- 多继承_方法解析顺序表MRO
c编译动态库可以编译但是无法导入解决方法
【面试】c++对象的存储空间
Python高级笔记(三) -- 私有化
Python高级笔记(二) -- 深拷贝和浅拷贝
python成长之路-----day1----笔记(1)
centos6搭建redis集群搭建(单机多节点)
Copyright © 2011-2022 走看看