CustomValidator in Gridview (itemtemplate): Server Side Validation not working

Last post 03-04-2009 6:09 AM by nabeelmoeen. 3 replies.

Sort Posts:

  • CustomValidator in Gridview (itemtemplate): Server Side Validation not working

    08-29-2008, 6:10 PM
    • Member
      73 point Member
    • dwilliams459
    • Member since 03-01-2006, 4:24 PM
    • Charlotte, NC
    • Posts 37

    I have a Custom Validator in a Gridview (Item Row), with server side validation, however I am not able to fire the Server side events.  Please reference http://forums.asp.net/t/1205910.aspx, where followed all of the steps to confirm the Custom Validator is setup.  The control does not use validation groups and the calling button causes other validation events (outside the gridview) to fire.  I have set ValidateEmptyText="true" and OnServerValidate="ServerValidateFunction".  On debug, the Server Validate function does not make it to the first line.  I have not been able to find anything when googling this issue. 

    So the question: Is it possible to bind a Custom Validator in a Gridview Item row to a client side validation function?  I should add this is inside an update pannel, however other events (outside the datagrid, but inside the update panel) work without issue). 

     Thanks in advance

    David Williams MCP
    .Net Senior Developer
  • Re: CustomValidator in Gridview (itemtemplate): Server Side Validation not working

    08-30-2008, 3:13 AM
    • Star
      9,464 point Star
    • kamii47
    • Member since 05-26-2005, 4:04 PM
    • Karachi, Pakistan
    • Posts 2,223
    Let us see your Aspx and code behind
    Kamran Shahid
    Sr. Software Engineer
    (MCP,MCAD.net,MCSD.net,MCTS,MCPD.net[web])

    Remember to click "Mark as Answer" on the post that helps U
  • Re: CustomValidator in Gridview (itemtemplate): Server Side Validation not working

    09-01-2008, 9:48 PM
    Answer
    • Member
      73 point Member
    • dwilliams459
    • Member since 03-01-2006, 4:24 PM
    • Charlotte, NC
    • Posts 37

    I got this to work.  Not in the original application that was having the issue (as this is a very large page), but in a simplified version.  Next I look at what is missing in my main application. 

    I created a web page with an update panel containing a validation summary, and a simple gridview and a button.  The gridview is populated by an XMLDataSource (for simplicity), and a column template containg two text boxes and a custom validator.  The Custom validator is 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 visiblity 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.
    • Update panel 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>

     

    David Williams MCP
    .Net Senior Developer
  • Re: CustomValidator in Gridview (itemtemplate): Server Side Validation not working

    03-04-2009, 6:09 AM
    • Member
      2 point Member
    • nabeelmoeen
    • Member since 03-04-2009, 6:07 AM
    • Posts 1

     Hi,

    thanks for the tip. I was able to get the event to fire by setting the ValidateEmptyText to true and clearing the ControlToValidate to blank, but even setting the isValid to false in the event code does not prevent the row to be added to the gridview :(

Page 1 of 1 (4 items)