asp.net usercontrol SetFocusOnError
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 use开发者_开发技巧rcontrol
thanks shannon
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.
精彩评论