Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. SharePoint
  4. Calling javascript for a SharePoint webpart?

Calling javascript for a SharePoint webpart?

Scheduled Pinned Locked Moved SharePoint
questionjavascriptsharepointtools
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Holdorf
    wrote on last edited by
    #1

    How do I call javascript from a SharePoint webpart. The webpart is just a user defined control ascx file. Where do I register the script section and where do I put the functions? Thanks, Steve Holdorf

    S N 2 Replies Last reply
    0
    • S Steve Holdorf

      How do I call javascript from a SharePoint webpart. The webpart is just a user defined control ascx file. Where do I register the script section and where do I put the functions? Thanks, Steve Holdorf

      S Offline
      S Offline
      Steve Holdorf
      wrote on last edited by
      #2

      I was told: You can create a Litral control and set the Text property, like following - protected override void CreateChildControls() { Litral scriptLitral=new Litral(); scriptLitral.Text="<script type='text\javascript'>"; scriptLitral.Text+="function CustomAlert(msg){"; scriptLitral.Text+="alert(msg);}</script>"; Controls.Add(scriptLitral); } I tried this but could not add the function to a button control. Can you help?

      1 Reply Last reply
      0
      • S Steve Holdorf

        How do I call javascript from a SharePoint webpart. The webpart is just a user defined control ascx file. Where do I register the script section and where do I put the functions? Thanks, Steve Holdorf

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        If it is an ASCX you can embed the script within the markup. Or you could do it from the code behind using the RegisterClientScriptBlock[^] or RegisterClientScriptInclude[^]


        Failure is not an option; it's the default selection.

        S 1 Reply Last reply
        0
        • N Not Active

          If it is an ASCX you can embed the script within the markup. Or you could do it from the code behind using the RegisterClientScriptBlock[^] or RegisterClientScriptInclude[^]


          Failure is not an option; it's the default selection.

          S Offline
          S Offline
          Steve Holdorf
          wrote on last edited by
          #4

          Thanks for your help. I can put the script in the ascx file just fine. The problem is that when I try to access document.getElementById('<%# input1.ClientID %>'.value I get the error input not found. It seems that the input control on the ascx can not be found. Thanks, Steve

          N 1 Reply Last reply
          0
          • S Steve Holdorf

            Thanks for your help. I can put the script in the ascx file just fine. The problem is that when I try to access document.getElementById('<%# input1.ClientID %>'.value I get the error input not found. It seems that the input control on the ascx can not be found. Thanks, Steve

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            Have you debugged? Have you used tools such as IE developer toolbar to inspect the elements? IMO, I would be using JQuery rather than document.getElementById. It is cross-browser and has better functionality.


            Failure is not an option; it's the default selection.

            S 1 Reply Last reply
            0
            • N Not Active

              Have you debugged? Have you used tools such as IE developer toolbar to inspect the elements? IMO, I would be using JQuery rather than document.getElementById. It is cross-browser and has better functionality.


              Failure is not an option; it's the default selection.

              S Offline
              S Offline
              Steve Holdorf
              wrote on last edited by
              #6

              I am using the IE developer toolbar to try to find my element but it's id is not being found. don't know if document.getElementById works with sharepoint textbox controls. How would you recommend getting the textbox element's value in the javascript? Thanks, Steve

              N 1 Reply Last reply
              0
              • S Steve Holdorf

                I am using the IE developer toolbar to try to find my element but it's id is not being found. don't know if document.getElementById works with sharepoint textbox controls. How would you recommend getting the textbox element's value in the javascript? Thanks, Steve

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                Of course document.getElementById works with SharePoint. SharePoint is ASP.NET and uses ASP.NET controls, but getElementById is JavaScript and works on the client-side. Perhaps you are not familiar with Dev toolbar operations. Click the HTML tab, then the arrow below it. Move to the IE window to highlight an element and click on it. You should see the details of the rendered element in the tool window. You can the find the ID that has been given to the rendered element. Using JQuery you can find an element using a selector, such as

                $("[id$='myElement']")

                This will find any element whose id ends with myElement, regardless of the name mangling applied by ASP.NET


                Failure is not an option; it's the default selection.

                S 1 Reply Last reply
                0
                • N Not Active

                  Of course document.getElementById works with SharePoint. SharePoint is ASP.NET and uses ASP.NET controls, but getElementById is JavaScript and works on the client-side. Perhaps you are not familiar with Dev toolbar operations. Click the HTML tab, then the arrow below it. Move to the IE window to highlight an element and click on it. You should see the details of the rendered element in the tool window. You can the find the ID that has been given to the rendered element. Using JQuery you can find an element using a selector, such as

                  $("[id$='myElement']")

                  This will find any element whose id ends with myElement, regardless of the name mangling applied by ASP.NET


                  Failure is not an option; it's the default selection.

                  S Offline
                  S Offline
                  Steve Holdorf
                  wrote on last edited by
                  #8

                  I used the Dev toolbar and it showed the id of the rendered element as 'txtMsg' which was the actual id in the VS markup. That is what I was using with the getElementById which it could not find. Now, I am confused. Sorry for the problems! Steve

                  S 1 Reply Last reply
                  0
                  • S Steve Holdorf

                    I used the Dev toolbar and it showed the id of the rendered element as 'txtMsg' which was the actual id in the VS markup. That is what I was using with the getElementById which it could not find. Now, I am confused. Sorry for the problems! Steve

                    S Offline
                    S Offline
                    Steve Holdorf
                    wrote on last edited by
                    #9

                    got it working. Sorry for being a pain in the butt.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups