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===================
 

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

Top