Using Javascript in UserControl

L

Lau Lei Cheong

Hello,

I'm writing a usercontrol to be included in my project. The usercontrol
have a few javascript function to do the client-side tasks. However, the
controls on the usercontrol seems to change at execution(e,g: the TextBox
with id & name"txtbox" in usercontrol "folder1" will have name
"folder1:txtbox" and id "folder1_txtbox" during execution)
Since I'll use the control in many place inside my project, it's not
possible for me to hardcode their name. Could anyone suggest some way to
address the controls?
The first idea come up in my mind is to use document.getElementById().
But then I do not know how to gather the ID of the usercontrol itself.
Any help would be appreciated. Thank you.

Regards,
Lau Lei Cheong
 
D

Darren Clark

If you want to konw the exact name of the controll that gets sent out in the
HTML

use the ClientID propertiy.

it will return the folder1_txtBox value for you .
 
L

Lau Lei Cheong

After some experiment, I finally come up with this piece of code.
Not using ClientID, but it does do the job. :)

var ctlname = name.substr(0, name.search(/:/i));
document.getElementById(ctlname + 'mycontrol').value='newvalue';

I use name property instead of Id to address the control because colon
is not commonly used in name, while underscore in Id does commonly used
in Id.

Regards,
Lau Lei Cheong
 

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,057
Latest member
KetoBeezACVGummies

Latest Threads

Top