467,118 Members | 988 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,118 developers. It's quick & easy.

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
  • viewed: 1255
Share:
4 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael Erb | last post: by
3 posts views Thread by Stephen Kellett | last post: by
9 posts views Thread by Lyners | last post: by
3 posts views Thread by Lyners | last post: by
4 posts views Thread by . | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.