How to use JavaScript to get server control's ID

G

Guest

Here is my case : I create a user control which contains One checkbox
control to enable/disable the other two textbox control. I'm using server
control instead of HTML control, and I don't want to post back to server to
do the disable.

The problem is I there is no easy way for me to find the ID in the
javascript since the ID or name are all prefixed with parent name. Even
worse, this control could be wrapped with another user control which stop me
to hardcode the ID.

I know I could create java function to accept control ID as input params ,
and use WebControl.clientID to form the function call on the server side.

Here I'm looking for if there is other alternative. Thanks
 
G

Guest

why not use an HTML control with a "runat=server" instead of the otherway
around? May make life easier.
 
J

James Doughty

I've come across this problem and depending on situation here are two
options that worked for me.

1. Generate the Javascript from you code.
This will allow you to use CheckBox.ClientID, TextBox.Client
That way you can generate you javascript and it will reference the
correct name.

2. Add an Attribute to the textboxes say CheckStatus = "TRUE" and then go
through each of the element in the body until you find the one you need.
 
S

sreejith.ram

i guess, using 'clientID' is the most common & easy method to deal with
javascript for user control elements..

Not sure why this scenario need passing "control ID as input params"
etc.. following 5 lines code would do the job.. (this may have minor
syntax errors)

strFun = " <script> function disablecheckbox() { "
strFun += txtControl1.ClientID + ".disabled = true"
strFun += txtControl2.ClientID + ".disabled = true"
strFun += " } </script> "
page.RegisterClientScript("name", str)
 
G

Guest

thanks for the help, Unfortunately seems we have to rely on server side logic.

Ram, reason I don't want to render javascript in codebehind,which I think
should be avoided always, is that I think it's more proper to store
javascript in a contral text file.--this will give developer the room to
update java logic without touching the code behind and recompile; second, you
approach wouldn't work when a page contains two instances from the same
control type.
 
S

sreejith.ram

yes, i too think it is better to keep javascript outside code behind
when ever possible..

regarding multiple instances of controls in same page - one way to
handle it without passing control id as input param to the javascript
function is to render different javascript functions for each instance
(would be ideal only if the javascript functions are simple like in
this case)

I would change the function header to

strFun = " <script> function disablecheckbox_" + this.ClientID + "()
{ "

this would render a differnt javascript for each instance of control.

not the most efficient method, but it lets me keep the javascript as a
part of control itself and simple to implement and debug.
 

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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top