473,811 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.clie ntID 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 22030
why not use an HTML control with a "runat=serv er" 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.clie ntID 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=serv er" 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.clie ntID 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.Client ID, 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*@morneausob eco.com> wrote in
news:5E******** *************** ***********@mic rosoft.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=serv er" 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.clie ntID 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.clie ntID 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.Cli entID + ".disabled = true"
strFun += txtControl2.Cli entID + ".disabled = true"
strFun += " } </script> "
page.RegisterCl ientScript("nam e", 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,whic h 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**********@g mail.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.clie ntID 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.Cli entID + ".disabled = true"
strFun += txtControl2.Cli entID + ".disabled = true"
strFun += " } </script> "
page.RegisterCl ientScript("nam e", 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**********@g mail.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

0
9728
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10648
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9205
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.