473,320 Members | 1,902 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,320 software developers and data experts.

Problem retreiving value in javascript

I have recently created a ASP site utilizing Master Pages and all works fine
until I want to proces my javascripts. Just to let you know, most of
cliewnt side scripting is new to me.

But anyway,

I can retrieve and updat ethe value a textbox on the page by using the

document.getElementById('Goals_Main_tbProduct0').v alue

by not by using

document.getElementById('<%=tbProduct0.ClientID%>' ).value

I would prefer to use the latter because I have read that is a much leaner
way to process just in case you page ever changes or if you ever add any
additional complexity to it. But evertime I try to retrieve the value it
returns a null value or if I try to set hte value I get Microsoft JScript
runtime error: 'document.getElementById(...)' is null or not an object.

Can anyone give me any advice, this is getting rather annoying.
Thanks

Aug 17 '06 #1
4 1548
Hi,

James Pemberton wrote:
I have recently created a ASP site utilizing Master Pages and all works fine
until I want to proces my javascripts. Just to let you know, most of
cliewnt side scripting is new to me.

But anyway,

I can retrieve and updat ethe value a textbox on the page by using the

document.getElementById('Goals_Main_tbProduct0').v alue

by not by using

document.getElementById('<%=tbProduct0.ClientID%>' ).value

I would prefer to use the latter because I have read that is a much leaner
way to process just in case you page ever changes or if you ever add any
additional complexity to it. But evertime I try to retrieve the value it
returns a null value or if I try to set hte value I get Microsoft JScript
runtime error: 'document.getElementById(...)' is null or not an object.

Can anyone give me any advice, this is getting rather annoying.
Thanks
What is the HTML/JavaScript code produced by the server? Open your page
in IE and then select View Source. This way, you can see what code the
server created, and that should help you to find the error.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Aug 17 '06 #2
Thank Laurent that pointed me in somewhat of a starting direction. I
originally had this script stored in an external JavaScript file in VS 2005
and the ClientID would not work, but I change my ASP page and imbedded the
script. When I then viewed it on the page it appeared to translate it
correctly.

So now my question is, how to I get my external script to work the same way?
Or can I?

I was referencing my other script like so:
<script src="Scripts/Goals_script.js" language="javascript"
type="text/javascript"></script>

Thanks

"Laurent Bugnion" <ga*********@bluewin.chwrote in message
news:e6**************@TK2MSFTNGP05.phx.gbl...
Hi,

James Pemberton wrote:
>I have recently created a ASP site utilizing Master Pages and all works
fine until I want to proces my javascripts. Just to let you know, most
of cliewnt side scripting is new to me.

But anyway,

I can retrieve and updat ethe value a textbox on the page by using the

document.getElementById('Goals_Main_tbProduct0'). value

by not by using

document.getElementById('<%=tbProduct0.ClientID%> ').value

I would prefer to use the latter because I have read that is a much
leaner way to process just in case you page ever changes or if you ever
add any additional complexity to it. But evertime I try to retrieve the
value it returns a null value or if I try to set hte value I get
Microsoft JScript runtime error: 'document.getElementById(...)' is null
or not an object.

Can anyone give me any advice, this is getting rather annoying.
Thanks

What is the HTML/JavaScript code produced by the server? Open your page in
IE and then select View Source. This way, you can see what code the server
created, and that should help you to find the error.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Aug 17 '06 #3
Hi,

James Pemberton wrote:
Thank Laurent that pointed me in somewhat of a starting direction. I
originally had this script stored in an external JavaScript file in VS 2005
and the ClientID would not work, but I change my ASP page and imbedded the
script. When I then viewed it on the page it appeared to translate it
correctly.

So now my question is, how to I get my external script to work the same way?
Or can I?

I was referencing my other script like so:
<script src="Scripts/Goals_script.js" language="javascript"
type="text/javascript"></script>

Thanks
OK, now I understand your problem better. It's really an understanding
problem: You need to understand that only the ASPX page is processed by
the server, and the <%= ... %syntax will only work when written in the
ASPX page itself. All linked files (CSS, JavaScript) will not be processed.

What you can do however is use a variable to define the textbox's client
ID. Since every bit JavaScript on your page and also the scripts defined
in external files run in the same engine, you can define a variable in
the ASPX page and use it inside your external script. For example:

In the ASPX page:

string strScript = "<script type=\"text/javascript\">"
+ Environment.NewLine
+ "var strTextboxId = '" + tbProduct0.ClientID + "';"
+ Environment.NewLine
+ "</script>";

this.ClientScript.RegisterClientScriptBlock( typeof( YourPage ),
"TextboxIdScript",
strScript );
Then in the JavaScript file:

if ( strTextboxId )
{
var nTextbox = document.getElementById( strTextboxId );
if ( nTextbox
&& nTextbox.value )
{
// Use nTextbox.value
}
}
Sorry, didn't have time to test.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Aug 17 '06 #4
Thanks again Laurent, you pointed me in a direction to actually utilize both
internal variables and external scripts.
Just for someone else's information that might run into this same problem.
I know this might not be the most efficient way, but it worked

I had a group of textboxes I wanted to process with various functions.
I create an array on my asp page defining each element as my client id for
the box:
TextBoxArray[1] = '<%=Textbox1.ClientID%>'
TextBoxArray[2] = '<%=Textbox2.ClientID%>'
and so on

Then in my external Javascript file I just had to reference the array to
obtain all of the client id names for my text boxes.

It's a good thing to learn soemthing new everyay!

"Laurent Bugnion" <ga*********@bluewin.chwrote in message
news:u1**************@TK2MSFTNGP06.phx.gbl...
Hi,

James Pemberton wrote:
>Thank Laurent that pointed me in somewhat of a starting direction. I
originally had this script stored in an external JavaScript file in VS
2005 and the ClientID would not work, but I change my ASP page and
imbedded the script. When I then viewed it on the page it appeared to
translate it correctly.

So now my question is, how to I get my external script to work the same
way? Or can I?

I was referencing my other script like so:
<script src="Scripts/Goals_script.js" language="javascript"
type="text/javascript"></script>

Thanks

OK, now I understand your problem better. It's really an understanding
problem: You need to understand that only the ASPX page is processed by
the server, and the <%= ... %syntax will only work when written in the
ASPX page itself. All linked files (CSS, JavaScript) will not be
processed.

What you can do however is use a variable to define the textbox's client
ID. Since every bit JavaScript on your page and also the scripts defined
in external files run in the same engine, you can define a variable in the
ASPX page and use it inside your external script. For example:

In the ASPX page:

string strScript = "<script type=\"text/javascript\">"
+ Environment.NewLine
+ "var strTextboxId = '" + tbProduct0.ClientID + "';"
+ Environment.NewLine
+ "</script>";

this.ClientScript.RegisterClientScriptBlock( typeof( YourPage ),
"TextboxIdScript",
strScript );
Then in the JavaScript file:

if ( strTextboxId )
{
var nTextbox = document.getElementById( strTextboxId );
if ( nTextbox
&& nTextbox.value )
{
// Use nTextbox.value
}
}
Sorry, didn't have time to test.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Aug 17 '06 #5

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

Similar topics

4
by: The Roys | last post by:
Hi I have a textbox which displays a formatted numeric. eg: 100,000 When I retreive this value for calculations, it is valued at 100 eg: calc = val(txtbox) * 2 calc will equal 200, not...
2
by: Reddy | last post by:
Hi, Once after inserting a record into a table is there a more efficient way of retreiving the id(autonumber field) value in the following example. Using a SQL server database. con.Open() ...
0
by: yogeeswar | last post by:
HI ALL I AM USING DB2 8.1 AND I HAVE TWO TABLES PARENT TABLE 1)CODE IS PRIMARY KEY CODE CODE_DESC CATEGORY ----------- ----------------------------------------...
0
by: kmithu | last post by:
Hey friends, I am making a calender control using javascript but I am not able to retrieve its value on my .aspx.cs coding page in asp.net2.0. My text box code is : <input type="text"...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.