Problem retreiving value in javascript

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

    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.getEle mentById('Goals _Main_tbProduct 0').value

    by not by using

    document.getEle mentById('<%=tb Product0.Client ID%>').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.getEl ementById(...)' is null or not an object.

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


    Thanks



  • Laurent Bugnion

    #2
    Re: Problem retreiving value in javascript

    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.getEle mentById('Goals _Main_tbProduct 0').value
    >
    by not by using
    >
    document.getEle mentById('<%=tb Product0.Client ID%>').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.getEl ementById(...)' 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

    Comment

    • James Pemberton

      #3
      Re: Problem retreiving value in javascript

      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="javas cript"
      type="text/javascript"></script>

      Thanks

      "Laurent Bugnion" <galasoft-lb@bluewin.chwr ote in message
      news:e6M7GgdwGH A.4972@TK2MSFTN GP05.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.getEl ementById('Goal s_Main_tbProduc t0').value
      >>
      >by not by using
      >>
      >document.getEl ementById('<%=t bProduct0.Clien tID%>').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.getEl ementById(...)' 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

      Comment

      • Laurent Bugnion

        #4
        Re: Problem retreiving value in javascript

        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="javas cript"
        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.New Line
        + "var strTextboxId = '" + tbProduct0.Clie ntID + "';"
        + Environment.New Line
        + "</script>";

        this.ClientScri pt.RegisterClie ntScriptBlock( typeof( YourPage ),
        "TextboxIdScrip t",
        strScript );


        Then in the JavaScript file:

        if ( strTextboxId )
        {
        var nTextbox = document.getEle mentById( 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

        Comment

        • James Pemberton

          #5
          Re: Problem retreiving value in javascript

          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.Cl ientID%>'
          TextBoxArray[2] = '<%=Textbox2.Cl ientID%>'
          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" <galasoft-lb@bluewin.chwr ote in message
          news:u1cM02fwGH A.1808@TK2MSFTN GP06.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="javas cript"
          >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.New Line
          + "var strTextboxId = '" + tbProduct0.Clie ntID + "';"
          + Environment.New Line
          + "</script>";
          >
          this.ClientScri pt.RegisterClie ntScriptBlock( typeof( YourPage ),
          "TextboxIdScrip t",
          strScript );
          >
          >
          Then in the JavaScript file:
          >
          if ( strTextboxId )
          {
          var nTextbox = document.getEle mentById( 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

          Comment

          Working...