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. Javascript in User controls

Javascript in User controls

Scheduled Pinned Locked Moved ASP.NET
javascriptwinformstutorial
5 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.
  • U Offline
    U Offline
    User 1804931
    wrote on last edited by
    #1

    Hello, I am creating a usercontrol which contains two DropDownList boxes, I want to access these controls in the javascript (i.e in ascx file) because I wrote two validation function for those two DropDownList boxes. I am able to do this in aspx file but I want to do it in .ascx file only. Please suggest me how to do this. Regards, Chakravarthy.

    M 1 Reply Last reply
    0
    • U User 1804931

      Hello, I am creating a usercontrol which contains two DropDownList boxes, I want to access these controls in the javascript (i.e in ascx file) because I wrote two validation function for those two DropDownList boxes. I am able to do this in aspx file but I want to do it in .ascx file only. Please suggest me how to do this. Regards, Chakravarthy.

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

      Hi there, If you are able to make it run when you put the client-side script in a web page .aspx, you also can do that when you move the script code to a web user control's content file .ascx. There's one thing that you need to pay your attention to is that the client script will be running on the client machine, so the id of the dropdownlist used in the validation function should be the clientid value but not the id of the dropdownlists declared in the web user control.

      U 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, If you are able to make it run when you put the client-side script in a web page .aspx, you also can do that when you move the script code to a web user control's content file .ascx. There's one thing that you need to pay your attention to is that the client script will be running on the client machine, so the id of the dropdownlist used in the validation function should be the clientid value but not the id of the dropdownlists declared in the web user control.

        U Offline
        U Offline
        User 1804931
        wrote on last edited by
        #3

        Hi, Thanks for your reply. I am attaching the code that I written and the exception that got. I declared two dropdownlist boxes in the MyControl.ascx i.e DropDownList1, DropdDownList2. I attached an attribute to the DropDownList1 i.e. this.DropDownList1.Attributes.Add("onchange"," findName (DropDownList1,DropDownList2)"); In the code behind of javascript block I written this code. function findName(List1,List2) { alert(List1.length); alert(List2.length); } The exception that I got is DropDownList1 is Undefined(JavaSript Exception); Please help me.

        M 1 Reply Last reply
        0
        • U User 1804931

          Hi, Thanks for your reply. I am attaching the code that I written and the exception that got. I declared two dropdownlist boxes in the MyControl.ascx i.e DropDownList1, DropdDownList2. I attached an attribute to the DropDownList1 i.e. this.DropDownList1.Attributes.Add("onchange"," findName (DropDownList1,DropDownList2)"); In the code behind of javascript block I written this code. function findName(List1,List2) { alert(List1.length); alert(List2.length); } The exception that I got is DropDownList1 is Undefined(JavaSript Exception); Please help me.

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

          Hi there, You'd better use the clientid of the control as the method parameter, the sample code looks something like this:

          this.DropDownList1.Attributes.Add("onchange"," findName('"+ this.DropDownList1.ClientID +"','"+ this.DropDownList2.ClientID +"')");

          In the client side function, you should first get a reference to an html element before accessing any properties and methods supported by this element.

          function findName(List1,List2)
          {
          alert(window.document.getElementById(List1).length);
          alert(window.document.getElementById(List2).length);
          }

          For more information, see Control.ClientID Property [^]

          U 1 Reply Last reply
          0
          • M minhpc_bk

            Hi there, You'd better use the clientid of the control as the method parameter, the sample code looks something like this:

            this.DropDownList1.Attributes.Add("onchange"," findName('"+ this.DropDownList1.ClientID +"','"+ this.DropDownList2.ClientID +"')");

            In the client side function, you should first get a reference to an html element before accessing any properties and methods supported by this element.

            function findName(List1,List2)
            {
            alert(window.document.getElementById(List1).length);
            alert(window.document.getElementById(List2).length);
            }

            For more information, see Control.ClientID Property [^]

            U Offline
            U Offline
            User 1804931
            wrote on last edited by
            #5

            Thank you very much for your help.It solved my problem. Regards, Chakravarthy.

            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