Using Javascript in UserControl

  • Thread starter Thread starter Lau Lei Cheong
  • Start date Start date
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
 
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 .
 
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
 
Back
Top