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
查看全文
相关阅读:
kioptrix-1
4.4 CSRF
upload-labs 练习笔记
4.3 XSS
外国人是怎样读编程书的呢?
如何快速学习新语言
Go开发环境配置
Golang Package I
MVC模式小结
Flask基础知识
原文地址:https://www.cnblogs.com/downmoon/p/1020089.html
最新文章
2019-2020学年 20191327《信息安全专业导论》第九周学习总结
2019-2020学年 20191327《信息安全专业导论》第八周学习总结
2019-2020学年 20191327《信息安全专业导论》第七周学习总结
2019-2020学年 20191327《信息安全专业导论》第六周学习总结
《信息安全系统设计与实现》学习笔记5
《信息安全系统设计与实现》学习笔记4
sort命令实践
发际线和我做队——团队展示
Linux C语言编程基础
MyOD
热门文章
《信息安全系统设计与实现》学习笔记3
《信息安全系统设计与实现》学习笔记2
《信息安全系统设计与实现》学习笔记1
《信息安全系统设计基础》第十二周学习总结
kioptrix-4
kioptrix-3
kioptrix-2
4.7 File Read & Download & Delete
4.6 File Inclusion
4.5 SSRF
Copyright © 2011-2022 走看看