user controls and javascript

D

Dune

Hi there,

I have a user control and I need to carry out some client-
side custom validation on the controls within the user
control (hope that makes sense :) )

So I've stuck a CustomValidator into the page and set
ClientValidationFunction to the name of my javascript
validation function. But this is where I get lost...how
does one refer to the controls within the user control?

Referring to these internal controls using "document.forms
(0)..." seems wrong as there is no form within the user
control itself (as the user control will reside in the
form element of the web form that it is added to) and i've
tried it out and this indeed doesn't work.

So, this suggests to me that I can't carry out validation
using javascript within the user control itself, but must
instead, do the validation in the web form containing the
user control.

I have a bunch of these user controls on a web form which
is why it would be really nice to be able to make them do
their own validation, but if I have to validate them in
the web form, I guess I'll need to iterate through them
somehow?

Any suggestions?


Cheers :)
 
J

JB

I'm in exactly the same boat - tried various ways but my JS is not
that strong so floundering a bit.

Anyone out there lend a hand?

TIA
 
R

Rasmus Rummel

Hi Dune

Your UserControl will have a property UniqueID and all Controls on that
UserControl (residing in you UserControl Controls collection) will have
properties like ClientID and UniqueID. Lets say you have a Textbox
WebControl on your UserControl and you give the textbox an ID="tbNested"
then your Textbox ClientID is also "tbNested", but your Textbox UniqueID is
your UserControl UniqueID concatenated with the TextBox ClientID, so in your
UserControl, the following will be true:
tbNested.UniqueID = this.UniqueID + tbNested.ClientID (=this.UniqueID +
"tbNested").
ASP.NET will automatically give your Textbox an Id-attribute in the html
stream and the value is exactly the UniqueID, this means you have a unique
reference to your Textbox on the client side. The Javascript must be emitted
from your UserControl, because it is in your UserControl you know the
UniqueID of your Textbox.

To emit javascript from a UserControl use eg. the
Page.RegisterClientScriptBlock("key", "[javascript]"), you can look
RegisterClientScriptBlock up in your .NET Documentation or on MSDN online.
Then you first get the grip of it, it is straightforward to connect
javascript with your controls through the UniqueID (however there are some
potential complicated problem with the Id-separator, if you run into that,
check out a Control id-separator and id-render-separator in the debugger -
rember to follow the hierarchy all the way up to Control).

Regards
Rasmus Rummel
www.menulab.com
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top