Validation check

There are node leaf flags in the tree. I need to check the tree. if at least one of the node is checked and no more than specfic (say 3 nodes) the number of nodes that the user can select. Note. Treeview is an asp.net view tree (not an ajax tree).

+3
source share
1 answer

Well, since you did not specify what type of verification you want, I will do both the client and server side. My TreeViewcalled tvTest
First add CustomValidatorto your Asp.Net page:

<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ClientValidate"
  ErrorMessage="CustomValidator" Display="Dynamic" OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>

. ControlToValidate.
script ( Asp.Net) :

<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>

, :

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;
  }
}

, , .

+11

Source: https://habr.com/ru/post/1711330/


All Articles