views:

339

answers:

2

I'm having a heck of a time trying to figure out how to implement validation in a ListView. The goal is to require the user to enter text in the comments TextBox, but only if the CheckBox is checked. Downside is that these controls are in the EditTemplate of a ListView. Below is a snippet of the relevant code portion of the EditTemplate:

<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 I could validate manually in the ItemUpdating event of the Listview, or somewhere else, and on a failure, alert the user of the error via a label or popup alert?

+1  A: 

You can use a customValidator.

ASPX

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

Server Side Validation

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

Clientside Validation

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

Sample taken from http://www.4guysfromrolla.com/articles/073102-1.aspx

MIchael Grassman
The example you gave is perfectly valid for normal cases, but doesn't deal with checking the state of other controls, or for that matter DataRepeater controls. I was thinking of using a CustomValidator but I don't see how I can check the state of the controls in a row of a DataRepeater, particularly not server-side.
Jim Dagg
Though it was a step in the right direction; my answer explains that I can get away with using EditItem exposed by a ListView, in tandem with the CustomValidator. Thanks for the assistance.
Jim Dagg
Thanks. I was going to provide some code to integrate into the ListView but ran out of time. Glad to hear you figured it out.
MIchael Grassman
A: 

In a catastrophic display of ineptitude, I managed to miss that the ListView exposes the property EditItem. Which means I can get away with

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

I can query the state of that and the text box using a CustomValidator, per Mr. Grassman's response.

Jim Dagg