CustomValidator inside Datagrid, DataList, DataRepeater

G

ghafranabbas

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===================
 
Joined
Jun 11, 2008
Messages
1
Reaction score
0
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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top