Conditional required field validation in an ASP.net ListView

Posted by Jim Dagg on Stack Overflow See other posts from Stack Overflow or by Jim Dagg
Published on 2010-05-25T20:09:45Z Indexed on 2010/05/25 20:11 UTC
Read the original article Hit count: 457

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about input-validation