javascript troubleshooting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MattB

    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='javas cript'> " & _
    "function Lbs2kg() " & _
    "{" & _
    "var Lbs = 0; " & _
    "Lbs = " & txtWeight.Clien tID & ".value; " & _
    txtWeight_m.Cli entID & ".value = Lbs*0.454; " & _
    "}" & _
    "</script>"
    Page.RegisterCl ientScriptBlock ("Lbs2kg", strLbs2kg)
    txtWeight.Attri butes.Add("onCh ange", "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
  • Chris Botha

    #2
    Re: javascript troubleshooting

    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_TextWeightCln tId as String
    then in your page load, do
    m_TextWeightCln tId = TextWeight.Clie ntID
    then to get hold of it in the Java script
    var theTxtBox = document.GetEle mentById("<%=m_ TextWeightClntI d%>");

    "MattB" <somedudeus@yah oo.com> wrote in message
    news:3ujnavF11c a53U1@individua l.net...[color=blue]
    > 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='javas cript'> " & _
    > "function Lbs2kg() " & _
    > "{" & _
    > "var Lbs = 0; " & _
    > "Lbs = " & txtWeight.Clien tID & ".value; " & _
    > txtWeight_m.Cli entID & ".value = Lbs*0.454; " & _
    > "}" & _
    > "</script>"
    > Page.RegisterCl ientScriptBlock ("Lbs2kg", strLbs2kg)
    > txtWeight.Attri butes.Add("onCh ange", "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[/color]


    Comment

    • Bruce Barker

      #3
      Re: javascript troubleshooting

      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" <somedudeus@yah oo.com> wrote in message
      news:3ujnavF11c a53U1@individua l.net...[color=blue]
      > 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='javas cript'> " & _
      > "function Lbs2kg() " & _
      > "{" & _
      > "var Lbs = 0; " & _
      > "Lbs = " & txtWeight.Clien tID & ".value; " & _
      > txtWeight_m.Cli entID & ".value = Lbs*0.454; " & _
      > "}" & _
      > "</script>"
      > Page.RegisterCl ientScriptBlock ("Lbs2kg", strLbs2kg)
      > txtWeight.Attri butes.Add("onCh ange", "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[/color]


      Comment

      • Chris Botha

        #4
        Re: javascript troubleshooting

        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.Clien tID & ".value; ......
        thus put a "Form1." in front.

        "MattB" <somedudeus@yah oo.com> wrote in message
        news:3ujnavF11c a53U1@individua l.net...[color=blue]
        > 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='javas cript'> " & _
        > "function Lbs2kg() " & _
        > "{" & _
        > "var Lbs = 0; " & _
        > "Lbs = " & txtWeight.Clien tID & ".value; " & _
        > txtWeight_m.Cli entID & ".value = Lbs*0.454; " & _
        > "}" & _
        > "</script>"
        > Page.RegisterCl ientScriptBlock ("Lbs2kg", strLbs2kg)
        > txtWeight.Attri butes.Add("onCh ange", "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[/color]


        Comment

        • MattB

          #5
          Re: javascript troubleshooting

          Thanks for the replies. I ended up with a variation of this idea. It works.

          Matt

          Chris Botha wrote:[color=blue]
          > 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_TextWeightCln tId as String
          > then in your page load, do
          > m_TextWeightCln tId = TextWeight.Clie ntID
          > then to get hold of it in the Java script
          > var theTxtBox = document.GetEle mentById("<%=m_ TextWeightClntI d%>");
          >
          > "MattB" <somedudeus@yah oo.com> wrote in message
          > news:3ujnavF11c a53U1@individua l.net...
          >[color=green]
          >>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='javas cript'> " & _
          >> "function Lbs2kg() " & _
          >> "{" & _
          >> "var Lbs = 0; " & _
          >> "Lbs = " & txtWeight.Clien tID & ".value; " & _
          >> txtWeight_m.Cli entID & ".value = Lbs*0.454; " & _
          >> "}" & _
          >> "</script>"
          >> Page.RegisterCl ientScriptBlock ("Lbs2kg", strLbs2kg)
          >> txtWeight.Attri butes.Add("onCh ange", "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[/color]
          >
          >
          >[/color]

          Comment

          Working...