471,123 Members | 850 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 software developers and data experts.

Client-side validation on user control

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
May 10 '06 #1
3 8817

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

"David Davis" <Da********@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
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

May 10 '06 #2
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" wrote:

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

"David Davis" <Da********@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
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


May 10 '06 #3
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" <Da********@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
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" wrote:

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

"David Davis" <Da********@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
>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


May 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by news.microsoft.com | last post: by
2 posts views Thread by Rhino | last post: by
2 posts views Thread by Delmar | last post: by
2 posts views Thread by J Huntley Palmer | last post: by
5 posts views Thread by Yossarian | last post: by
2 posts views Thread by nsaffary | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.