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 find control in Javascript

How to find control in Javascript

Scheduled Pinned Locked Moved ASP.NET
questionjavascriptdatabasetutorial
11 Posts 4 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.
  • V Offline
    V Offline
    varshavmane
    wrote on last edited by
    #1

    Hi All, I have to find a control through Javascript which I have generate dynamically through codebehind. For ex: DropDownList ddlCombo = new DropDownList(); ddlCombo.ID = "Combo1"; ddlCombo.CssClass = "text"; ddlCombo.Width = 250; I need to find the ddlCombo Control in Javascript with it selected index. How do I do this?? Thanks in advance...

    P P S 3 Replies Last reply
    0
    • V varshavmane

      Hi All, I have to find a control through Javascript which I have generate dynamically through codebehind. For ex: DropDownList ddlCombo = new DropDownList(); ddlCombo.ID = "Combo1"; ddlCombo.CssClass = "text"; ddlCombo.Width = 250; I need to find the ddlCombo Control in Javascript with it selected index. How do I do this?? Thanks in advance...

      P Offline
      P Offline
      Parwej Ahamad
      wrote on last edited by
      #2

      In which container you added ddlCombo control ?

      Parwej Ahamad g.parwez@gmail.com

      V 1 Reply Last reply
      0
      • P Parwej Ahamad

        In which container you added ddlCombo control ?

        Parwej Ahamad g.parwez@gmail.com

        V Offline
        V Offline
        varshavmane
        wrote on last edited by
        #3

        Thanks for the reply. I am adding the control in tab container again tab is generated dynamically. It works fine with I run on my machine but it doesnt work on other machine since the ID is different. Please tell me how do I do this... Thanks again...

        1 Reply Last reply
        0
        • V varshavmane

          Hi All, I have to find a control through Javascript which I have generate dynamically through codebehind. For ex: DropDownList ddlCombo = new DropDownList(); ddlCombo.ID = "Combo1"; ddlCombo.CssClass = "text"; ddlCombo.Width = 250; I need to find the ddlCombo Control in Javascript with it selected index. How do I do this?? Thanks in advance...

          P Offline
          P Offline
          Pallavi Bhoite
          wrote on last edited by
          #4

          Hi, you need to find that control on any event or what ? you can use 2 ways : 1. In code behind itself u can add attribute to control e.g you can use button click event btnAdd.Attributes["onclick"] = "javascript:DisableControl('"+ddlCombo.ClientID+"');"; and in javascript if(document.getElementById(ddvalue).getAttribute("selectedIndex") or u can directly pass selected index ito javascript 2. in your javascript document.getElementById('Combo1').getAttribute ("selectedIndex") will give u control's index but i m not sure.

          pallavi

          V 1 Reply Last reply
          0
          • P Pallavi Bhoite

            Hi, you need to find that control on any event or what ? you can use 2 ways : 1. In code behind itself u can add attribute to control e.g you can use button click event btnAdd.Attributes["onclick"] = "javascript:DisableControl('"+ddlCombo.ClientID+"');"; and in javascript if(document.getElementById(ddvalue).getAttribute("selectedIndex") or u can directly pass selected index ito javascript 2. in your javascript document.getElementById('Combo1').getAttribute ("selectedIndex") will give u control's index but i m not sure.

            pallavi

            V Offline
            V Offline
            varshavmane
            wrote on last edited by
            #5

            Hi Pallavi, Thanks for the reply but I have done this and it wroks fine. But when I upload my project on server the ID is different and therefore my javascript function doesnt work. Thats is why I need to use .ClientID. All the controls on my form are generate dynamically. Please help me... Thanks again...

            1 Reply Last reply
            0
            • V varshavmane

              Hi All, I have to find a control through Javascript which I have generate dynamically through codebehind. For ex: DropDownList ddlCombo = new DropDownList(); ddlCombo.ID = "Combo1"; ddlCombo.CssClass = "text"; ddlCombo.Width = 250; I need to find the ddlCombo Control in Javascript with it selected index. How do I do this?? Thanks in advance...

              S Offline
              S Offline
              Sandeep Akhare
              wrote on last edited by
              #6

              After adding this DropDownlist to the content i think you can get its client id Check it

              Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

              V 1 Reply Last reply
              0
              • S Sandeep Akhare

                After adding this DropDownlist to the content i think you can get its client id Check it

                Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                V Offline
                V Offline
                varshavmane
                wrote on last edited by
                #7

                Hey Sandeep, thanks the reply. I am doing something like this : var Country = $find('<%=ddlCountry.ClientID%>'); but it says The name 'ddlCountry' does not exist in the current context Since the control in not available in .acsx as I have created it in code behind. I hope I am doing right???

                S 1 Reply Last reply
                0
                • V varshavmane

                  Hey Sandeep, thanks the reply. I am doing something like this : var Country = $find('<%=ddlCountry.ClientID%>'); but it says The name 'ddlCountry' does not exist in the current context Since the control in not available in .acsx as I have created it in code behind. I hope I am doing right???

                  S Offline
                  S Offline
                  Sandeep Akhare
                  wrote on last edited by
                  #8

                  Might be ddlCounty is not accessible there as it must be local variable in any method can you post the code related to drop down list ?

                  Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                  V 1 Reply Last reply
                  0
                  • S Sandeep Akhare

                    Might be ddlCounty is not accessible there as it must be local variable in any method can you post the code related to drop down list ?

                    Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                    V Offline
                    V Offline
                    varshavmane
                    wrote on last edited by
                    #9

                    Thanks again. Here is the code: I am adding this DropDownList to tab Container: DropDownList ddlCountry = new DropDownList(); ddlCountry.ID = "ddlCountry"; ddlCountry.CssClass = "text"; ddlCountry.Width = 250; tab1.Controls.Add(ddlCountry); how do I get this ID in Javascript??

                    S 2 Replies Last reply
                    0
                    • V varshavmane

                      Thanks again. Here is the code: I am adding this DropDownList to tab Container: DropDownList ddlCountry = new DropDownList(); ddlCountry.ID = "ddlCountry"; ddlCountry.CssClass = "text"; ddlCountry.Width = 250; tab1.Controls.Add(ddlCountry); how do I get this ID in Javascript??

                      S Offline
                      S Offline
                      Sandeep Akhare
                      wrote on last edited by
                      #10

                      Check the id of DDL after adding to tab like tab1.Controls.Add(ddlCountry); ddlCounty.Clientid; that will give you client id Now it depends on your code how are you adding this tab to the main content page

                      Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                      1 Reply Last reply
                      0
                      • V varshavmane

                        Thanks again. Here is the code: I am adding this DropDownList to tab Container: DropDownList ddlCountry = new DropDownList(); ddlCountry.ID = "ddlCountry"; ddlCountry.CssClass = "text"; ddlCountry.Width = 250; tab1.Controls.Add(ddlCountry); how do I get this ID in Javascript??

                        S Offline
                        S Offline
                        Sandeep Akhare
                        wrote on last edited by
                        #11

                        Another way instead of using ASP dropdown list you can use HtmlSelect Its id will changed runtime you can give data source and datamember to it HtmlSelect ddl = new HtmlSelect(); ddl.Items.Add(new ListItem("Sandeep","Sandeep")); ddl.ID="Sandeep"; PlaceHolder1.Controls.Add(this.Button1); PlaceHolder1.Controls.Add(ddl);

                        Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                        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