I cannot get a web user control to validation client-side using a required
field validator. It only validates server side.
I have a web user control in .net 2.0. I have a single text box on it. I
have a public Text property defined below:
<ValidationProperty("Text")> _
Partial Class MyWebUserControl
Inherits System.Web.UI.UserControl
Public Property Text() As String
Get
Return TextBox1.Text
End Get
Set(ByVal value As String)
TextBox1.Text = value
End Set
End Property
End Class
I have a RequiredFieldValidator on the same web page as my user control. I
put the user control's id in the ControlToValidate property of the validator
(please note that I must type it in, it doesn't show up in the dropdown list).
The control validates only server-side, not client-side. The html source
for my page contains the following:
<div>
<input name="MyWebUserControl1$TextBox1" type="text"
id="MyWebUserControl1_TextBox1" />
<span id="RequiredFieldValidator1" style="color:Red;">The user
control text is required.</span
</div>
....
<script type="text/javascript">
<!--
var RequiredFieldValidator1 = document.all ?
document.all["RequiredFieldValidator1"] :
document.getElementById("RequiredFieldValidator1") ;
RequiredFieldValidator1.controltovalidate = "MyWebUserControl1";
RequiredFieldValidator1.errormessage = "The user control text is required.";
RequiredFieldValidator1.isvalid = "False";
RequiredFieldValidator1.evaluationfunction =
"RequiredFieldValidatorEvaluateIsValid";
RequiredFieldValidator1.initialvalue = "";
// -->
</script>
Notice that the id for the control is "MyWebUserControl1_TextBox1" and the
javascript is just calling it "MyWebUserControl1". I'm not sure if this is a
problem or not.
Any suggestions? --David