Conditional Mandatory Field Validation in ASP.NET ListView

I have time trying to figure out how to implement validation in a ListView. The goal is to require the user to enter text in TextBox comments, but only if CheckBox is installed. The downside is that these controls are in the EditTemplate ListView. The following is a snippet of the corresponding part of the EditTemplate code:

<tr style="background-color: #00CCCC; color: #000000">
    <td>
        Assume Risk?
        <asp:CheckBox ID="chkWaive" runat="server" 
             Checked='<%# Bind("Waive") %>' />
    </td>
    <td colspan="5">
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
             runat="server" ErrorMessage="Comments required" 
             ControlToValidate="txtComments" />
        <asp:TextBox Width="95%" ID="txtComments" runat="server" 
             Text='<%# Eval("Comment") %>'></asp:TextBox>
    </td>
    <td>
        <asp:Button ID="btnSave" runat="server"
             Text="Save" CommandName="Update" Width="100px" />
    </td>
</tr>

Is there a way to do conditional validation using this method? If not, is there a way that I could check manually in the ItemUpdating event in a Listview or elsewhere, and if it fails, warn the user about the error with a label or a pop-up warning?

+3
source share
2

, ListView EditItem. ,

CType(ListView1.EditItem.FindControl("chkWaive"),CheckBox).Checked

, CustomValidator, - .

+1

CustomValidator.

ASPX

<asp:CustomValidator runat="server" id="custPrimeCheck"
        ControlToValidate="txtPrimeNumber"
        OnServerValidate="PrimeNumberCheck"
        ClientValidationFunction="CheckPrime"
        ErrorMessage="Invalid Prime Number" />

Sub PrimeNumberCheck(sender as Object, args as ServerValidateEventArgs)
    Dim iPrime as Integer = Cint(args.Value), iLoop as Integer, _
        iSqrt as Integer = CInt(Math.Sqrt(iPrime))

    For iLoop = 2 to iSqrt
      If iPrime mod iLoop = 0 then
        args.IsValid = False
        Exit Sub
      End If
    Next

    args.IsValid = True
End Sub

<script language="JavaScript">
<!--
  function CheckPrime(sender, args)
  {
    var iPrime = parseInt(args.Value);
    var iSqrt = parseInt(Math.sqrt(iPrime));

    for (var iLoop=2; iLoop<=iSqrt; iLoop++)
      if (iPrime % iLoop == 0) 
      {
         args.IsValid = false;
         return;
      }

    args.IsValid = true;
  }
// -->
</script>

, http://www.4guysfromrolla.com/articles/073102-1.aspx

+4

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


All Articles