Client-side validation on user control

G

Guest

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
 
M

MSDN

RequiredFieldValidator1.controltovalidate = TextBox1.ClientID() or
"MyWebUserControl1_TextBox1";
 
G

Guest

I tried this already and I get a "Unable to find control id
'WebUserControl1_TextBox1' referenced by the 'ControlToValidate' property of
'RequiredFieldValidator1'" error, even when I expose the textbox in my user
control as a property.

MSDN said:
RequiredFieldValidator1.controltovalidate = TextBox1.ClientID() or
"MyWebUserControl1_TextBox1";

David Davis said:
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
 
P

Parag

Hi,
You can solve the problem by setting the name property for the textbox and
then using document.getelementbyname function in JavaScript should be able
to get the control into some variable. Once you get hold of the control on
the client-side then application of any validation function shouldn't
present you any problem.

Regards
Parag Kulkarni
MTS
Persistent Systems Private Limited




David Davis said:
I tried this already and I get a "Unable to find control id
'WebUserControl1_TextBox1' referenced by the 'ControlToValidate' property
of
'RequiredFieldValidator1'" error, even when I expose the textbox in my
user
control as a property.

MSDN said:
RequiredFieldValidator1.controltovalidate = TextBox1.ClientID() or
"MyWebUserControl1_TextBox1";

David Davis said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top