【ASP.NET】动态绑定验证控件
一个CheckBoxList可以如此验证
以上是验证表单(form)中所有的checkbox是有一个被选中,引自金色約定之家 。
如果我想验证多个CheckBoxList呢?
方案一:动态输出JavaScript
表示 HTML 元素、文本和 ASP.NET 页中不需要在服务器上处理的任何其他字符串。
JavaScript同样属于HTML元素。可以输出到页面。
以下是生成的JavaScript代码:
方案二:判断验证控件的Id
1.在aspx文件中加入javascript
要验证的CheckBoxList相同,指定的验证控件只验证和自己ID主要部分相同的CheckBoxList.
2.添加自定义验证控件(CustomValidater),并将其ClientValidationFunction 属性设为ClientValidate,并指定其Id
要是把一些控件及其相关的验证都写到一个用户控件(ascx)里,是不是就不会互相影响了?
1.在aspx文件中添加javascript
<script language="javascript" type="text/javascript">
<!--
function ClientValidate(sender, args)
{
var flag = false;
var inarr=form1.all.tags("input");
for (var i=0; i<inarr.length; i++)
{
if(inarr[i].type=="checkbox")
{
if(inarr[i].checked==true)
{
flag = true;
}
}
}
if (flag)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
-->
</script>
<!--
function ClientValidate(sender, args)
{
var flag = false;
var inarr=form1.all.tags("input");
for (var i=0; i<inarr.length; i++)
{
if(inarr[i].type=="checkbox")
{
if(inarr[i].checked==true)
{
flag = true;
}
}
}
if (flag)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
-->
</script>
2.添加自定义验证控件(CustomValidater),并将其ClientValidationFunction
属性设为
Dim customValidatorVer As New CustomValidator
customValidatorVer.ClientValidationFunction = "ClientValidate"
customValidatorVer.ClientValidationFunction = "ClientValidate"
以上是验证表单(form)中所有的checkbox是有一个被选中,引自金色約定之家 。
如果我想验证多个CheckBoxList呢?
方案一:动态输出JavaScript
1.建立生成JavaScript的函数
'''<summary>
'''指定CheckBoxList的验证函数
'''</summary>
'''<param name="inCheckboxList">需要验证的控件</param>
'''<returns>完整的JavaScrip字符串</returns>
Private Function GetCheckScript(ByVal inCheckboxList As CheckBoxList) As String
Dim newLine As String = Chr(13) & Chr(10)
Dim strScript As New StringBuilder
strScript.Append("<script language='javascript' type='text/javascript'>")
strScript.Append(newLine)
strScript.Append("function CheckSearch_" & inCheckboxList.ClientID & "(sender, args)")
strScript.Append(newLine)
strScript.Append("{" & newLine & "with(document.forms[0])")
strScript.Append(newLine & "{" & newLine)
strScript.Append(newLine & "var flag=false;")
Dim i As Integer
For i = 0 To inCheckboxList.Items.Count - 1
strScript.Append(newLine)
strScript.Append("if(" + inCheckboxList.ClientID + "_" + i.ToString() + ".checked) {flag=true;}")
Next
strScript.Append(newLine)
strScript.Append("if(!flag) {args.IsValid = false;")
strScript.Append(newLine)
strScript.Append("}")
strScript.Append(newLine)
strScript.Append("else")
strScript.Append(newLine)
strScript.Append("{args.IsValid = true;")
strScript.Append(newLine)
strScript.Append("}")
strScript.Append(newLine & "}" & newLine & "}" & newLine)
strScript.Append("</script>")
Return strScript.ToString
End Function
'''指定CheckBoxList的验证函数
'''</summary>
'''<param name="inCheckboxList">需要验证的控件</param>
'''<returns>完整的JavaScrip字符串</returns>
Private Function GetCheckScript(ByVal inCheckboxList As CheckBoxList) As String
Dim newLine As String = Chr(13) & Chr(10)
Dim strScript As New StringBuilder
strScript.Append("<script language='javascript' type='text/javascript'>")
strScript.Append(newLine)
strScript.Append("function CheckSearch_" & inCheckboxList.ClientID & "(sender, args)")
strScript.Append(newLine)
strScript.Append("{" & newLine & "with(document.forms[0])")
strScript.Append(newLine & "{" & newLine)
strScript.Append(newLine & "var flag=false;")
Dim i As Integer
For i = 0 To inCheckboxList.Items.Count - 1
strScript.Append(newLine)
strScript.Append("if(" + inCheckboxList.ClientID + "_" + i.ToString() + ".checked) {flag=true;}")
Next
strScript.Append(newLine)
strScript.Append("if(!flag) {args.IsValid = false;")
strScript.Append(newLine)
strScript.Append("}")
strScript.Append(newLine)
strScript.Append("else")
strScript.Append(newLine)
strScript.Append("{args.IsValid = true;")
strScript.Append(newLine)
strScript.Append("}")
strScript.Append(newLine & "}" & newLine & "}" & newLine)
strScript.Append("</script>")
Return strScript.ToString
End Function
2.引用如上函数
Dim aLiteral As New LiteralControl
aLiteral.Text = GetCheckScript(CheckBoxList1)
Panel1.Controls.Add(aLiteral)
LiteralControl类aLiteral.Text = GetCheckScript(CheckBoxList1)
Panel1.Controls.Add(aLiteral)
表示 HTML 元素、文本和 ASP.NET 页中不需要在服务器上处理的任何其他字符串。
JavaScript同样属于HTML元素。可以输出到页面。
3.添加自定义验证控件,并绑定验证函数(ClientValidationFunction
)
Dim aValidator As New CustomValidator
aValidator.ClientValidationFunction = "CheckSearch_" & CheckBoxList1.ClientID
aValidator.Text = "Please Check the Checkbox!"
Panel1.Controls.Add(aValidato )
aValidator.ClientValidationFunction = "CheckSearch_" & CheckBoxList1.ClientID
aValidator.Text = "Please Check the Checkbox!"
Panel1.Controls.Add(aValidato )
以下是生成的JavaScript代码:
<script language="javascript" type="text/javascript">
function CheckSearch_CheckBoxList1()
{
with(document.forms[0])
{
var flg=false;
if(CheckBoxList1_0.checked) {flg=true;}
if(CheckBoxList1_1.checked) {flg=true;}
if(CheckBoxList1_2.checked) {flg=true;}
if(CheckBoxList1_3.checked) {flg=true;}
if(CheckBoxList1_4.checked) {flg=true;}
if(CheckBoxList1_5.checked) {flg=true;}
if(CheckBoxList1_6.checked) {flg=true;}
if(CheckBoxList1_7.checked) {flg=true;}
if(!flg)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
}
</script>
这样就可以验证动态生成的CheckBoxList1,CheckBoxList2,CheckBoxList3。。。。。了
function CheckSearch_CheckBoxList1()
{
with(document.forms[0])
{
var flg=false;
if(CheckBoxList1_0.checked) {flg=true;}
if(CheckBoxList1_1.checked) {flg=true;}
if(CheckBoxList1_2.checked) {flg=true;}
if(CheckBoxList1_3.checked) {flg=true;}
if(CheckBoxList1_4.checked) {flg=true;}
if(CheckBoxList1_5.checked) {flg=true;}
if(CheckBoxList1_6.checked) {flg=true;}
if(CheckBoxList1_7.checked) {flg=true;}
if(!flg)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
}
</script>
方案二:判断验证控件的Id
1.在aspx文件中加入javascript
<script language="javascript" type="text/javascript">
<!--
function ClientValidate(sender, args)
{
var flag = false;
var e = document.form1.elements;
for (var i=0; i<e.length; i++)
{
if(e[i].type=="checkbox" && e[i].name.substr(0,sender.id.length-2) == sender.id.substr(2,sender.id.length-2))
{
if(e[i].checked==true && e[i].name.substr(0,3) =="chk")
{
flag = true;
}
}
}
if (flag)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
-->
</script>
e[i].name.substr(0,sender.id.length-2) == sender.id.substr(2,sender.id.length-2)) 判断了验证控件的id是否主要部分和<!--
function ClientValidate(sender, args)
{
var flag = false;
var e = document.form1.elements;
for (var i=0; i<e.length; i++)
{
if(e[i].type=="checkbox" && e[i].name.substr(0,sender.id.length-2) == sender.id.substr(2,sender.id.length-2))
{
if(e[i].checked==true && e[i].name.substr(0,3) =="chk")
{
flag = true;
}
}
}
if (flag)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
-->
</script>
要验证的CheckBoxList相同,指定的验证控件只验证和自己ID主要部分相同的CheckBoxList.
2.添加自定义验证控件(CustomValidater),并将其ClientValidationFunction 属性设为ClientValidate,并指定其Id
Dim customValidatorVer As New CustomValidator
customValidatorVer.ClientValidationFunction = "ClientValidate"
customValidatorVer.Id=“V_” & CheckBoxList1.ClientID
customValidatorVer.ClientValidationFunction = "ClientValidate"
customValidatorVer.Id=“V_” & CheckBoxList1.ClientID
要是把一些控件及其相关的验证都写到一个用户控件(ascx)里,是不是就不会互相影响了?
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通