Geeks With Blogs
David Williams Who's scruffy looking?

Recently I needed to create a CustomValidator inside a GridView.  In the end this was relatively straight forward, however I had some problems in my main application.  As I could not find any examples of this on the Internet, I am documenting this here. 

As a proof of concept I created a web page with an UpdatePanel containing a ValidationSummary, a simple GridView and a button.  The GridView was populated by an XMLDataSource (for simplicity), and a column template containing two text boxes and a custom validator.  The Custom validator was validated via code on the server side.  The validation summary correctly displayed all error messages.  I found the following:

  • I did have to set the CustomValidators ValidateEmptyText=True, However ControlToValidate did not have to be set.  This was important as control visibility changed based on the data items.
  • In the CustomValidator_ServerValidate function, I was able to use CustomValidator.NamingContainer to get the Containing GridView Row.  From there I was able to get all of the controls on that row.
  • I did not have to do any binding in the GridView databound event to make this work. For example I did not have to bind the Custom Validator to the ControlToValidate using the Contols ClientID.  I saw talk on some forums about binding the custom validator when building the GridView.
  • The UpdatePanel did not make a difference.

Here is the Code:

 

 

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>

        <div>

            <asp:ValidationSummary ID="ValidationSummary1" runat="server" />

        </div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

                DataSourceID="XmlDataSource1">

            <Columns>

                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

                <asp:TemplateField HeaderText="Validation Test">

                    <ItemTemplate>

                        1:<asp:TextBox ID="TextBox1" runat="server"

                           Visible='<%# Eval("Enter1") %>'></asp:TextBox>

                        &nbsp;2:<asp:TextBox ID="TextBox2" runat="server"

                            Visible='<%# Eval("Enter2") %>' ></asp:TextBox>

                        <asp:CustomValidator ID="CustomValidator1" runat="server"

                            ErrorMessage="CustomValidator" Text="*"

                            onservervalidate="CustomValidator1_ServerValidate"

                            ValidateEmptyText="True"></asp:CustomValidator>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

        <asp:Button ID="Button1" runat="server" Text="Submit" />

    </ContentTemplate>

</asp:UpdatePanel>

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile1.xml">

</asp:XmlDataSource>

 

Partial Public Class _Default : Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

 

    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, _

             ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)

        Dim cv As CustomValidator = CType(source, CustomValidator)

        Dim gvr As GridViewRow = cv.NamingContainer

        Dim txt1 As TextBox = gvr.FindControl("TextBox1")

        Dim txt2 As TextBox = gvr.FindControl("TextBox2")

 

        'If String.IsNullOrEmpty(txt1.Text) Or String.IsNullOrEmpty(txt2.Text) Then

        If (txt1.Visible And String.IsNullOrEmpty(txt1.Text)) Or _

                (txt2.Visible And String.IsNullOrEmpty(txt2.Text)) Then

            cv.ErrorMessage = String.Format("Please enter text on row {0}", gvr.RowIndex)

            args.IsValid = False

        End If

    End Sub

End Class

 

<?xml version="1.0" encoding="utf-8" ?>

<customers>

    <customer Id="1" Enter1="true" Enter2="true"/>

    <customer Id="2" Enter1="true" Enter2="false"/>

    <customer Id="3" Enter1="false" Enter2="true"/>

    <customer Id="4" Enter1="false" Enter2="false"/>

</customers>

 

 

 

Posted on Thursday, September 4, 2008 9:51 AM Asp.Net , AJAX | Back to top


Comments on this post: CustomValidator in a GridView with a ValidationSummary control

# re: CustomValidator in a GridView with a ValidationSummary control
Requesting Gravatar...
Just want to say that this post helped me a lot because I encountered the very same problem. How would I guess that ValidateEmptyText needs to be set when I am to validate a checkbox.
Left by nap on Feb 04, 2009 9:44 PM

# re: CustomValidator in a GridView with a ValidationSummary control
Requesting Gravatar...
I am doing something similar. When editing the validator triggers but it doesn't show me any message. It goes through the validate method twice, and each time it comes out false it just doesn't do the update. It just shows me the grid row as if I haven't made any changes. And NO MESSAGE. I'm beggining to think this is a VS2005 bug.
Left by Dani on Aug 07, 2009 7:36 AM

# re: CustomValidator in a GridView with a ValidationSummary control
Requesting Gravatar...
Thank you. This helped me very much.
Left by Pete on Oct 10, 2013 4:24 AM

Your comment:
 (will show your gravatar)


Copyright © David Williams | Powered by: GeeksWithBlogs.net