views:

42

answers:

1

I have a aspx page that several of the same usercontrols on the page. The usercontrol houses a textbox that has a Required field validator on it. The validator works but the setonfocus="true" does not seem to be working, further more, the button the aspx page when the validator shows the error message, the button still fires the code behind.

Here is what the aspx page looks like as far as the user control and the the button.

ucTB:ucTextBox ID="ucTextR" runat="server" ValidationGroup="txtRequired" Required="_true" 

asp:Button ID="btnSave" runat="server" Text="Click" ValidationGroup="txtRequired" 

and the usercontrol validator

asp:RequiredFieldValidator ID="rfTextBox" runat="server" ControlToValidate="txtTextBox"
SetFocusOnError="true" ErrorMessage="Required Field"  EnableClientScript="false"  

the user control has been wired to grab the validator from the aspx page and use it in the usercontrol... something like this

 Public Property ValidationGroup() As String  
    Get  
        Return CType(ViewState("ValidationGroup"), String)  
    End Get  
    Set(ByVal Value As String)  
        ViewState("ValidationGroup") = Value  
    End Set  
End Property

Protected Sub AssignValidation()
    For Each control As Control In Me.Controls
        Dim [property] As PropertyInfo = control.[GetType]().GetProperty("ValidationGroup")
        If [property] Is Nothing Then
            Continue For
        End If
        [property].SetValue(control, ValidationGroup, Nothing)
    Next
End Sub

and i load the AssignValidation on page_load

anyway.. hope this is the info you need to point me in the right direction.

What i'm looking to do is if the required field validator to put the focus on the usercontrol if there is nothing in the usercontrol text box and also for the button on the aspx page not to fire.. like i think it behaves if you use a validator on a aspx page with no usercontrol

thanks shannon

A: 

Hey,

You can't set the user control to visible because it's not a visible container. You can set the focus yourself programmatically. See this:

http://forums.digitalpoint.com/showthread.php?t=282224

Or, you can programmably set the validator to the ID of the textbox within the user control; expose a textboxID property in the user control code-behind which returns the textbox's ID, and have your page assign the validator's controltovalidateID to this.

Brian
thanks for the response.. i'm not trying to set the user control to visable. What i was hoping to do was that the button on the form would not fire it's event if the validation saw an error. That is the way it seems to work when a usercontrol is not in the picture. But with the usercontrol, the validation fires but if validation fails, the button event also fires. I'd like for it not to do that, jst as it doesn't when validation fails on a non usercontrolled event.
jvcoach23