树形验证 [英] Treeview validation
问题描述
树形视图有叶节点checkboxes.I需要验证树状
如果该节点的用于至少一个被选中并且不大于节点specfic(比如3个节点)的数量更多的用户可以选择。
注:树视图是一个asp.net树形(而不是一个Ajax TreeView控件)
The treeview has leaf node checkboxes.I need to validate the treeview if atleast one of the node is checked and not more than a specfic(say 3 nodes) number of nodes a user can select. Note:The Treeview is a asp.net treeview(not an ajax treeview)
推荐答案
好吧,因为你没有提到你想要什么类型的验证,我会做客户端端和服务器端。我的 TreeView控件
被命名为 tvTest
首先,添加一个的CustomValidator
来你Asp.Net页:
Alright, since you didn't mentioned what type of validation you want, I'll do both client side and server side. My TreeView
is named tvTest
First, add a CustomValidator
to you Asp.Net page:
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ClientValidate"
ErrorMessage="CustomValidator" Display="Dynamic" OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
注:没有设置的ControlToValidate
接下来,客户端验证添加这个脚本(也到你的Asp.Net页):
Note: don't set the ControlToValidate
property.
Next, add this script (also to your Asp.Net page) for client side validation:
<script type="text/javascript">
function ClientValidate(source, arguments) {
var treeView = document.getElementById("<%= tvTest.ClientID %>");
var checkBoxes = treeView.getElementsByTagName("input");
var checkedCount = 0;
for (var i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i].checked) {
checkedCount++;
}
}
if (checkedCount > 0 && checkedCount < 4) {
arguments.IsValid = true;
} else {
arguments.IsValid = false;
}
}
</script>
而在去年,这对于服务器端验证添加到您的code-背后:
And last, add this to your code-behind for server side validation:
protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args) {
if (tvTest.CheckedNodes.Count > 0 && tvTest.CheckedNodes.Count < 4) {
args.IsValid = true;
} else {
args.IsValid = false;
}
}
当然,你要改变的最小和节点的最大数量的用户可以检查限制。
Of course, you'll want to change the limits for the minimum and maximum number of nodes the user can check.
这篇关于树形验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!