473,385 Members | 2,003 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.

javascript troubleshooting

I'm just trying to do some simple javascript in a form to convert Lbs in
one textbox to kg in another. I'm doing the following inside a If Not
IsPostBack block:

Dim strLbs2kg As String
strLbs2kg = "<script language='javascript'> " & _
"function Lbs2kg() " & _
"{" & _
"var Lbs = 0; " & _
"Lbs = " & txtWeight.ClientID & ".value; " & _
txtWeight_m.ClientID & ".value = Lbs*0.454; " & _
"}" & _
"</script>"
Page.RegisterClientScriptBlock("Lbs2kg", strLbs2kg)
txtWeight.Attributes.Add("onChange", "Lbs2kg()")

But when I type into my txtWeight box and tab off, the kg field is not
populated and I get an script error in the browser saying:

Error: 'txtWeight' is undefined
Code: 0

I thought the ClientID were automatically generated. Id that not true?
Any hints on making this work? Thanks!

Matt
Nov 23 '05 #1
4 1439
The ClientID is only valid in the code-behind, so you must declare a
variable up top in your class that can be accessed from the Java script,
like
Protected m_TextWeightClntId as String
then in your page load, do
m_TextWeightClntId = TextWeight.ClientID
then to get hold of it in the Java script
var theTxtBox = document.GetElementById("<%=m_TextWeightClntId%>") ;

"MattB" <so********@yahoo.com> wrote in message
news:3u*************@individual.net...
I'm just trying to do some simple javascript in a form to convert Lbs in
one textbox to kg in another. I'm doing the following inside a If Not
IsPostBack block:

Dim strLbs2kg As String
strLbs2kg = "<script language='javascript'> " & _
"function Lbs2kg() " & _
"{" & _
"var Lbs = 0; " & _
"Lbs = " & txtWeight.ClientID & ".value; " & _
txtWeight_m.ClientID & ".value = Lbs*0.454; " & _
"}" & _
"</script>"
Page.RegisterClientScriptBlock("Lbs2kg", strLbs2kg)
txtWeight.Attributes.Add("onChange", "Lbs2kg()")

But when I type into my txtWeight box and tab off, the kg field is not
populated and I get an script error in the browser saying:

Error: 'txtWeight' is undefined
Code: 0

I thought the ClientID were automatically generated. Id that not true? Any
hints on making this work? Thanks!

Matt

Nov 23 '05 #2
your code looks correct. is txtWeight visible? if not, it won't render and
the client code will fail. if its not visible switch to a hidden field.

-- bruce (sqlwork.com)
"MattB" <so********@yahoo.com> wrote in message
news:3u*************@individual.net...
I'm just trying to do some simple javascript in a form to convert Lbs in
one textbox to kg in another. I'm doing the following inside a If Not
IsPostBack block:

Dim strLbs2kg As String
strLbs2kg = "<script language='javascript'> " & _
"function Lbs2kg() " & _
"{" & _
"var Lbs = 0; " & _
"Lbs = " & txtWeight.ClientID & ".value; " & _
txtWeight_m.ClientID & ".value = Lbs*0.454; " & _
"}" & _
"</script>"
Page.RegisterClientScriptBlock("Lbs2kg", strLbs2kg)
txtWeight.Attributes.Add("onChange", "Lbs2kg()")

But when I type into my txtWeight box and tab off, the kg field is not
populated and I get an script error in the browser saying:

Error: 'txtWeight' is undefined
Code: 0

I thought the ClientID were automatically generated. Id that not true? Any
hints on making this work? Thanks!

Matt

Nov 23 '05 #3
Oops, sorry, you are building the Java script in the code-behind, so it will
have the right ID, ignore my message above.
The Java script will not find the boxes though, do it like
"Lbs = Form1." & txtWeight.ClientID & ".value; ......
thus put a "Form1." in front.

"MattB" <so********@yahoo.com> wrote in message
news:3u*************@individual.net...
I'm just trying to do some simple javascript in a form to convert Lbs in
one textbox to kg in another. I'm doing the following inside a If Not
IsPostBack block:

Dim strLbs2kg As String
strLbs2kg = "<script language='javascript'> " & _
"function Lbs2kg() " & _
"{" & _
"var Lbs = 0; " & _
"Lbs = " & txtWeight.ClientID & ".value; " & _
txtWeight_m.ClientID & ".value = Lbs*0.454; " & _
"}" & _
"</script>"
Page.RegisterClientScriptBlock("Lbs2kg", strLbs2kg)
txtWeight.Attributes.Add("onChange", "Lbs2kg()")

But when I type into my txtWeight box and tab off, the kg field is not
populated and I get an script error in the browser saying:

Error: 'txtWeight' is undefined
Code: 0

I thought the ClientID were automatically generated. Id that not true? Any
hints on making this work? Thanks!

Matt

Nov 24 '05 #4
Thanks for the replies. I ended up with a variation of this idea. It works.

Matt

Chris Botha wrote:
The ClientID is only valid in the code-behind, so you must declare a
variable up top in your class that can be accessed from the Java script,
like
Protected m_TextWeightClntId as String
then in your page load, do
m_TextWeightClntId = TextWeight.ClientID
then to get hold of it in the Java script
var theTxtBox = document.GetElementById("<%=m_TextWeightClntId%>") ;

"MattB" <so********@yahoo.com> wrote in message
news:3u*************@individual.net...
I'm just trying to do some simple javascript in a form to convert Lbs in
one textbox to kg in another. I'm doing the following inside a If Not
IsPostBack block:

Dim strLbs2kg As String
strLbs2kg = "<script language='javascript'> " & _
"function Lbs2kg() " & _
"{" & _
"var Lbs = 0; " & _
"Lbs = " & txtWeight.ClientID & ".value; " & _
txtWeight_m.ClientID & ".value = Lbs*0.454; " & _
"}" & _
"</script>"
Page.RegisterClientScriptBlock("Lbs2kg", strLbs2kg)
txtWeight.Attributes.Add("onChange", "Lbs2kg()")

But when I type into my txtWeight box and tab off, the kg field is not
populated and I get an script error in the browser saying:

Error: 'txtWeight' is undefined
Code: 0

I thought the ClientID were automatically generated. Id that not true? Any
hints on making this work? Thanks!

Matt


Nov 24 '05 #5

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

Similar topics

3
by: Michael Erb | last post by:
I've got a page with links to panoramic images and use javascript to open a separate window when the link to a specific panoramic image is clicked. Problem is that it's not working when I visit...
3
by: Stephen Kellett | last post by:
Hi Folks, With Mozilla there is a well documented Javascript debugging API that you can call from C/C++. I've been trying to identify if there is a documented API that you can use for the same...
9
by: Lyners | last post by:
Quick question. I have some java script that looks like this; ...
3
by: Lyners | last post by:
I have a table within a cell of a datagrid. I am doing updates without postback to the server using Javascript. I have everything working, except referencing a table within the datagrid cell. ...
3
by: Robert | last post by:
Hi, I'm interested in two applications for javascript. A profiler and a minimizer. I found several, but I am hoping to hear with which applications some of you guys have a good experience. I...
4
by: . | last post by:
The future will be JSython. Can you help this project?
4
omerbutt
by: omerbutt | last post by:
hi everyone can any one guide me if there is any add-on related to troubleshooting javascript errors in IE6 regards, Omer Aslam
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.