CustomValidator inside Datagrid, DataList, DataRepeater

Discussion in 'ASP .Net' started by ghafranabbas@gmail.com, Mar 11, 2005.

  1. Guest

    This is how you use the customvalidator control in any INamingContainer
    interface control (Datagrid, DataList, DataRepeater, etc).

    1. In the ItemTemplate, place your customvalidator
    2. Set the OnServerValidate property of the customvalidator
    3. Get reference to the Data Item object
    (For DataList is DataListItem, for DataGrid its DataGridItem)
    4. Get reference to any controls needed for validation
    5. Perform the validation

    Below is an example, where the two textboxes inside the ItemTemplate of
    a DataList control cannot have the same value.

    '=============Begin Code===================

    <script runat="server">
    Private Sub ValidateCustomValidator(ByVal source As System.Object,
    ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)

    Dim DataItem As DataListItem = CType(source,
    CustomValidator).NamingContainer
    Dim txt1 As TextBox = DataItem.FindControl("TextBox1")
    Dim txt2 As TextBox = DataItem.FindControl("TextBox2")
    If txt1.Text = txt2.Text Then
    args.IsValid = False
    End If
    End Sub
    </script>

    <asp:DataList id="DataList1" runat="server">
    <ItemTemplate>
    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
    <asp:CustomValidator id="CustomValidator1" runat="server"
    ErrorMessage="Cannot Be The Same"
    OnServerValidate="ValidateCustomValidator">*</asp:CustomValidator>
    </ItemTemplate>
    </asp:DataList>

    '=============End Code===================
    , Mar 11, 2005
    #1
    1. Advertising

  2. Tmez

    Joined:
    Jun 11, 2008
    Messages:
    1
    What if you validation Control (RequiredFieldValidator, RegularExpressionValidator)

    Since those controls do not expose the OnserverValidate property, I had to find an alternate method.

    1. Use any validation control that you want
    2. On the code behind, use Page.Validate()
    a. Page.Validate() will force the entire page to validate again; hence, trigger your validation controls to take action again 8) .
    3. If you don't want the entire page then simply use Page.Validate (ValidationGroupName) where ValidationGroupName is the validation group name in your validation controls.
    4. Now I am waiting for the client-side way to do this please...
    Tmez, Jun 11, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Shelly
    Replies:
    1
    Views:
    427
    Lee Atkinson
    Oct 16, 2003
  2. Nevyn Twyll
    Replies:
    8
    Views:
    1,869
  3. Steve
    Replies:
    0
    Views:
    3,051
    Steve
    Feb 1, 2005
  4. Les Caudle
    Replies:
    0
    Views:
    1,988
    Les Caudle
    May 2, 2006
  5. Trauckos

    CustomValidator inside Datagrid

    Trauckos, Oct 29, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    139
    Fernando Hunth
    Dec 10, 2004
Loading...

Share This Page