By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,991 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,991 IT Pros & Developers. It's quick & easy.

Client-side validation on user control

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

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

P: n/a
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

P: n/a
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.