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. how to access server control from javascript

how to access server control from javascript

Scheduled Pinned Locked Moved ASP.NET
javascriptsysadminhelptutorial
4 Posts 3 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.
  • E Offline
    E Offline
    enjoycrack
    wrote on last edited by
    #1

    hi, I have a custom server control (.ascx file) that contains hidden textbox in a table. After that, in another page I use this control and i want to generate javascript to access this hidden textbox (my javascript code to access it: documet.framse[1].frmName.txtbox.value, but it works incorrectly) if Anyone know it, Pls help me. Thanks a lot :zzz:

    A J 2 Replies Last reply
    0
    • E enjoycrack

      hi, I have a custom server control (.ascx file) that contains hidden textbox in a table. After that, in another page I use this control and i want to generate javascript to access this hidden textbox (my javascript code to access it: documet.framse[1].frmName.txtbox.value, but it works incorrectly) if Anyone know it, Pls help me. Thanks a lot :zzz:

      A Offline
      A Offline
      Andrew Quinn AUS
      wrote on last edited by
      #2

      Hi there, If you have a hidden textbox in a user control (say with an id of "Hidden1") and the id of the WebUserControl, when you insert it into the aspx page, is for example "WebUserControl11", e.g.

      <uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>

      Then when the page is finally rendered, that hidden textbox will have an id of "WebUserControl11_Hidden1". So if in the aspx page you have some javascript, then something like the following will access it...

      function getHiddenTextValue()
      {
      strValue = "";
      var elHidden = document.getElementById("WebUserControl11_Hidden1");
      if (elHidden)
      {
      strValue = elHidden.value;
      }
      return strValue;
      }

      Hope this helps, Andy

      E 1 Reply Last reply
      0
      • E enjoycrack

        hi, I have a custom server control (.ascx file) that contains hidden textbox in a table. After that, in another page I use this control and i want to generate javascript to access this hidden textbox (my javascript code to access it: documet.framse[1].frmName.txtbox.value, but it works incorrectly) if Anyone know it, Pls help me. Thanks a lot :zzz:

        J Offline
        J Offline
        Jesse Squire
        wrote on last edited by
        #3

        A Web Control acts as a naming container for its sub-controls. What this means is that your textbox will be renamed on the client side in order to ensure that it is unique. This is the case for both your custom control and the framework supplied ASP.NET controls. Assume for a moment that you dropped two of your custom controls on a page. If the hidden textbox name was not changed, you would have two controls with the id 'hidden1' on the page. What I would recommend is that you make your hidden input a server control (use runat="server"), and then add a property to retrieve the ClientID property. You could then use this property to dynamically retrieve the id of your hidden field. Code Behind:

        namespace MyWebControl
        {
        public class MyControl : System.Web.UI.UserControl
        {
        protected System.Web.UI.HtmlControls.HtmlInputHidden hidden1;

        public string HiddenClientName
        {
          get {return hidden1.ClientID;}
        }  
        

        }
        }

        Client Script:

        function getHiddenTextValue()
        {
        strValue = "";

        1 Reply Last reply
        0
        • A Andrew Quinn AUS

          Hi there, If you have a hidden textbox in a user control (say with an id of "Hidden1") and the id of the WebUserControl, when you insert it into the aspx page, is for example "WebUserControl11", e.g.

          <uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>

          Then when the page is finally rendered, that hidden textbox will have an id of "WebUserControl11_Hidden1". So if in the aspx page you have some javascript, then something like the following will access it...

          function getHiddenTextValue()
          {
          strValue = "";
          var elHidden = document.getElementById("WebUserControl11_Hidden1");
          if (elHidden)
          {
          strValue = elHidden.value;
          }
          return strValue;
          }

          Hope this helps, Andy

          E Offline
          E Offline
          enjoycrack
          wrote on last edited by
          #4

          thanks a lot:eek:

          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