views:

1089

answers:

4

I got a Gridview in an UpdatePanel with this EditTemplate:

<edititemtemplate>
    <asp:textbox id="txtDistFrom" runat="server" text='<%# Bind("distFrom") %>' width="30" />
    <asp:CustomValidator ID="valDistFrom" ValidateEmptyText="True" OnServerValidate="valDistFromTo_ServerValidate" ControlToValidate="txtDistFrom" Text="Missing" ToolTip="Invalid" Display="Dynamic" runat="server" />
</edititemtemplate>

And a simple Server-side function:

Protected Sub valDistFromTo_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 tbV As UI.WebControls.TextBox = gvr.FindControl("txtDistFrom")
    If tbV.Text <> "" Then
     args.IsValid = False
     cv.ErrorMessage = "inhalt ist " & tbV.Text
    End If
End Sub

But when debugging this code the server-side function is not fired, whatever it does. It seems it has to do with the gridview, so I cannot access the control directly by its id. Any suggestions?

A: 

The problem is related to the ControlToValidate property, because the ID of your text box is not used in repeating elements like GridView, ListView and Repeater. In other words: You have stumbled upon a limitation in ASP.NET's engine.

I am not sure how to solve this problem, though. You might be able do it, by adding the CustomValidator programmatically by attaching a method to the GridView's OnRowBound method.

This article might provide an answer This article might provide an answer: Integrating Asp.Net Validation Controls with GridView at run-time.

Jan Aagaard
If this is right, I cannot use validation controls in gridview at all. Other types, i.e. a RangeValidator also have ControlToValidate properties and they work.
Ulli
A: 

In this case you can use a required field validator. Which should work just fine in a grid.

For server side validation I would move the custom validator outside the grid entirely and leave the ControlToValidate property blank. You can move your validation to the RowUpdating event of the grid and set any error messages on the custom validator. Rmember to set the validators IsValid property appropriately.

TGnat
Thanks, that worked, also with the controlValidator inside the Gridview.
Ulli
A: 

I also tend to think that ControlToValidate is the problem. .NET changes the ID of that control at runtime and the custom validator probably isn't picking it up.

I would try adding the customvalidator on RowCreated or RowDatabound using the FindControl()

scottschulthess
A: 

If you modify your VB to:

Protected Sub valDistFromTo_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim cv As CustomValidator = CType(source, CustomValidator)
        If args.Value <> "" Then
            args.IsValid = False
            cv.ErrorMessage = "inhalt ist " & args.Value
        End If
End Sub

It should work. Note that I'm using args.Value. I use CustomValidators and TextBox within EditTemplates with ControlToValidate set to the TextBox ID all the time and it works, you just can't get the TextBox object the way you're trying it. I think this is far less of a pain and much cleaner than messing around with RowUpdating Event as suggested above.

Jason Kostempski
Thanks, that works too. I first got problems using that method in the footertemplate too. But its important to fire the Page.Validate method in the Handler methods für updating and inserting.
Ulli