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. ASP.NET
  4. Custom Control Javascript Issues

Custom Control Javascript Issues

Scheduled Pinned Locked Moved ASP.NET
helpcsharpjavascriptdata-structurestools
4 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.
  • P Offline
    P Offline
    perlmunger
    wrote on last edited by
    #1

    I have a custom control that renders a tree view. Each node on the tree has a hyperlink. The issue is that I cannot get a handle to the hyperlink object to set the onclick attribute for firing my javascript function. I can, however, set the URL, so what I am ending up with is setting the url to point to a javascript funcion like this:

    <a href="javascript:PostIt(4)">Send Request</a>

    (I put a colon in between javascript and PostIt, but it's not showing up on this page properly, btw). and a script that looks like this:

    function PostIt(folderID)
    {
    ___ctl0__ctl0__ctl0_mainTreeView_State.value=folderID;
    __doPostBack('_ctl0:_ctl0:_ctl0:mainTreeView','___ctl0__ctl0__ctl0_mainTreeView_State.value');
    }

    The value '___ctl0__ctl0__ctl0_mainTreeView_State' was created by this call in my C# code:

    Page.RegisterHiddenField(HelperID, selectedFolderID );

    The error message I get, though is, "Error: PostIt is not defined Source File: javascript:PostIt(4) Line: 1". Any ideas as to why it would be saying that this function is not defined (the script block was added to the page through Page.RegisterClientScriptBlock)? Thanks in advance. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

    M 1 Reply Last reply
    0
    • P perlmunger

      I have a custom control that renders a tree view. Each node on the tree has a hyperlink. The issue is that I cannot get a handle to the hyperlink object to set the onclick attribute for firing my javascript function. I can, however, set the URL, so what I am ending up with is setting the url to point to a javascript funcion like this:

      <a href="javascript:PostIt(4)">Send Request</a>

      (I put a colon in between javascript and PostIt, but it's not showing up on this page properly, btw). and a script that looks like this:

      function PostIt(folderID)
      {
      ___ctl0__ctl0__ctl0_mainTreeView_State.value=folderID;
      __doPostBack('_ctl0:_ctl0:_ctl0:mainTreeView','___ctl0__ctl0__ctl0_mainTreeView_State.value');
      }

      The value '___ctl0__ctl0__ctl0_mainTreeView_State' was created by this call in my C# code:

      Page.RegisterHiddenField(HelperID, selectedFolderID );

      The error message I get, though is, "Error: PostIt is not defined Source File: javascript:PostIt(4) Line: 1". Any ideas as to why it would be saying that this function is not defined (the script block was added to the page through Page.RegisterClientScriptBlock)? Thanks in advance. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      I suspect that the error happens due to the following code line:

      ___ctl0__ctl0__ctl0_mainTreeView_State.value=folderID;

      Here, you are trying to access the hidden element via its name, and it may cause the 'undefined' script error. You can try again to get the value of the hidden emelent via its id as below:

      window.document.getElementById('___ctl0__ctl0__ctl0_mainTreeView_State').value=folderID;

      P 1 Reply Last reply
      0
      • M minhpc_bk

        I suspect that the error happens due to the following code line:

        ___ctl0__ctl0__ctl0_mainTreeView_State.value=folderID;

        Here, you are trying to access the hidden element via its name, and it may cause the 'undefined' script error. You can try again to get the value of the hidden emelent via its id as below:

        window.document.getElementById('___ctl0__ctl0__ctl0_mainTreeView_State').value=folderID;

        P Offline
        P Offline
        perlmunger
        wrote on last edited by
        #3

        This was a good suggestion. It led me to discover that you cannot access a hidden element using getElementById unless you've given that element an ID. It won't work using the name unless you specify the whole hierarchy (document.form.hiddenfieldname). So instead of registering my hidden field with Page.RegisterHiddenField, I just manually add the hidden input type with an ID field. This actually works. Thanks a bunch. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

        M 1 Reply Last reply
        0
        • P perlmunger

          This was a good suggestion. It led me to discover that you cannot access a hidden element using getElementById unless you've given that element an ID. It won't work using the name unless you specify the whole hierarchy (document.form.hiddenfieldname). So instead of registering my hidden field with Page.RegisterHiddenField, I just manually add the hidden input type with an ID field. This actually works. Thanks a bunch. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Hi Matt, In fact, you can keep using the Page.RegisterHiddenField method to register your hidden field as there are a couple of alternative ways to access a hidden element, for example: the document.getElementsByName method or the document.all collection. You can take a quick look at the document object here in MSDN http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_document.asp[^]

          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