How to call function with params in external JS file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SteveW
    New Member
    • Mar 2007
    • 4

    How to call function with params in external JS file

    I'm working with ASP.NET 2.0 and Javascript. I started out OK by attaching the JS code to the object in the PageLoad(). Note that rbInputStyle is a RadioButtonList .

    rbInputStyle.It ems(0).Attribut es.Add("onclick ", lblPrice.Client ID
    + ".innerHTML=""U nit Price"";")
    rbInputStyle.It ems(1).Attribut es.Add("onclick ", lblPrice.Client ID
    + ".innerHTML=""T otal Proceeds"";")

    That worked great. Now I need to move the javascript into a seperate external file, since it needs to get a bit more involved and I would like to reuse it. I tried the following:

    rbInputStyle.It ems(0).Attribut es.Add("onclick ", "ShowPrice( " + lblPrice.Client ID
    + ");")
    rbInputStyle.It ems(1).Attribut es.Add("onclick ", "ShowProcee ds(" +
    lblPrice.Client ID + ");")

    In the aspx source file I added the following:

    <script language="JavaS cript" src="MyJSLib.js "></script>

    Finally, in MyJSLib.js I created two functions:

    function showPrice(lbl)
    {
    lbl.innerHTML=" Unit Price";
    }

    function showProceeds(lb l)
    {
    lbl.innerHTML=" Total Proceeds";
    }

    This is where it breaks down. I can't seem to get the above code to work. If I can get past this, I should have no problems passing in the multiple controls.
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    what errors is this bringing up?



    I didn't catch any complications off the top of my head just at a glance.

    Comment

    • SteveW
      New Member
      • Mar 2007
      • 4

      #3
      No error is generated, it just doesn't work. Seems like the code is not being called. Also, I didn't think it was related so I left out the fact that these are contained within a ASP.NET 2.0 Master Page Content Holder. It didn't affect the code when I embedded it in the HTML, but maybe it affects the parameter passing.

      Comment

      • SteveW
        New Member
        • Mar 2007
        • 4

        #4
        Oops, I was wrog there is an error, sort of. Using IE, a warning icon appears at the bottom of the page informing me that there are errors on the page. The only information in the details for the error is the message "Object Expected".

        Comment

        • iam_clint
          Recognized Expert Top Contributor
          • Jul 2006
          • 1207

          #5
          ok then i think that its not passing the correct ID

          rbInputStyle.It ems(0).Attribut es.Add("onclick ", "ShowPrice( ' + lblPrice.Client ID
          + ');")

          try this...

          Comment

          • SteveW
            New Member
            • Mar 2007
            • 4

            #6
            Thanks, but this did not help. I also tried many other variations on passing the name of the control, even as a literal, ie.

            Add("onclick", "ShowPrice(lblP rice);")
            Add("onclick", "ShowPrice(ctl0 0$ContentPlaceH older1$lblPrice );")
            Add("onclick", "ShowPrice(ctl0 0_ContentPlaceH older1_lblPrice );")

            I'm starting to think that the problem is that its not finding the .JS file.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Yes, most likely it's not finding the external .js file. See this link for a solution.

              Comment

              Working...