473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to use JavaScript to get server control's ID

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
Nov 19 '05 #1
7 21994
why not use an HTML control with a "runat=server" instead of the otherway
around? May make life easier.

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"walter" wrote:
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

Nov 19 '05 #2
Tested, no good. ID and name are prefixed for any server control.

"Curt_C [MVP]" wrote:
why not use an HTML control with a "runat=server" instead of the otherway
around? May make life easier.

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"walter" wrote:
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

Nov 19 '05 #3
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.
=?Utf-8?B?d2FsdGVy?= <ww*@morneausobeco.com> wrote in
news:5E**********************************@microsof t.com:
Tested, no good. ID and name are prefixed for any server control.

"Curt_C [MVP]" wrote:
why not use an HTML control with a "runat=server" instead of the
otherway around? May make life easier.

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"walter" wrote:
> 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


Nov 19 '05 #4
i guess, using 'clientID' is the most common & easy method to deal with
javascript for user control elements..
> 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.


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)

Nov 19 '05 #5
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.
"sr**********@gmail.com" wrote:
i guess, using 'clientID' is the most common & easy method to deal with
javascript for user control elements..
> 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.


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)

Nov 19 '05 #6
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.

Nov 19 '05 #7
Ram, we have exactly the same thinking path.Thanks for the opinion.

"sr**********@gmail.com" wrote:
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.

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.