Problem retreiving value in javascript

J

James Pemberton

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
 
L

Laurent Bugnion

Hi,

James said:
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
 
J

James Pemberton

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
 
L

Laurent Bugnion

Hi,

James said:
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
 
J

James Pemberton

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!
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top