admin管理员组文章数量:1291050
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 treeview(not an 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 treeview(not an ajax treeview)
Share Improve this question edited Jun 27, 2009 at 6:13 Greg Hewgill 995k192 gold badges1.2k silver badges1.3k bronze badges asked Jun 26, 2009 at 9:36 skamaleskamale1 Answer
Reset to default 11Alright, 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>
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>
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.
本文标签: cTreeview validationStack Overflow
版权声明:本文标题:c# - Treeview validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515484a2382860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论