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 get the value of the dropdownlist selected

how to get the value of the dropdownlist selected

Scheduled Pinned Locked Moved ASP.NET
javascripthelptutorialquestion
10 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.
  • L Offline
    L Offline
    lakshmichawala
    wrote on last edited by
    #1

    iam adding items to the dropdownlist using javascript function while loading the page.using the webservice.now when click on save button how can i get the value selected in the dropdownlist in the code behind,my problem is that the value selected in the dropdownlist is getting null when i accessing through code behind

    A 1 Reply Last reply
    0
    • L lakshmichawala

      iam adding items to the dropdownlist using javascript function while loading the page.using the webservice.now when click on save button how can i get the value selected in the dropdownlist in the code behind,my problem is that the value selected in the dropdownlist is getting null when i accessing through code behind

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      Show the code for both JavaScript and Codebehind ( just adding item in Dropdown and Retreving from it ).

      cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net My Latest Article : IIS Remote Debugging

      L 1 Reply Last reply
      0
      • A Abhijit Jana

        Show the code for both JavaScript and Codebehind ( just adding item in Dropdown and Retreving from it ).

        cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net My Latest Article : IIS Remote Debugging

        L Offline
        L Offline
        lakshmichawala
        wrote on last edited by
        #3

        in javascript ============= <script type="text/javascript" language ="javascript" > function AddItem() { var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="One"; opt.value="One" var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="Two"; opt.value="Two" var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="Three"; opt.value="Three"; return false; } function GetDropDownValue() { alert("h") var ddlValue; var objControl = document.getElementById('<%=DropDownList1.ClientID %>'); alert(objControl.selectedIndex) ddlValue=objControl.options[o bjControl.selectedIndex].value; document.getElementById('hfldResult').value = ddlValue; } </script> in code behind Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim tJavaScript As String = "" tJavaScript = "<script language='javascript'>GetDropDownValue();</script>" Page.ClientScript.RegisterStartupScript(Me.GetType, "abc", tJavaScript) End Sub

        C A B 3 Replies Last reply
        0
        • L lakshmichawala

          in javascript ============= <script type="text/javascript" language ="javascript" > function AddItem() { var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="One"; opt.value="One" var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="Two"; opt.value="Two" var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="Three"; opt.value="Three"; return false; } function GetDropDownValue() { alert("h") var ddlValue; var objControl = document.getElementById('<%=DropDownList1.ClientID %>'); alert(objControl.selectedIndex) ddlValue=objControl.options[o bjControl.selectedIndex].value; document.getElementById('hfldResult').value = ddlValue; } </script> in code behind Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim tJavaScript As String = "" tJavaScript = "<script language='javascript'>GetDropDownValue();</script>" Page.ClientScript.RegisterStartupScript(Me.GetType, "abc", tJavaScript) End Sub

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          lakshmichawala wrote:

          document.getElementById('hfldResult').value = ddlValue;

          So, are you accessing this control, or the drop list in your code behind ? You have not posted that code, that I can see.

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          L 1 Reply Last reply
          0
          • C Christian Graus

            lakshmichawala wrote:

            document.getElementById('hfldResult').value = ddlValue;

            So, are you accessing this control, or the drop list in your code behind ? You have not posted that code, that I can see.

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            L Offline
            L Offline
            lakshmichawala
            wrote on last edited by
            #5

            ya in hidden field iam giving the ddl selected value but iam getting null while accessing the value

            C 1 Reply Last reply
            0
            • L lakshmichawala

              in javascript ============= <script type="text/javascript" language ="javascript" > function AddItem() { var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="One"; opt.value="One" var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="Two"; opt.value="Two" var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="Three"; opt.value="Three"; return false; } function GetDropDownValue() { alert("h") var ddlValue; var objControl = document.getElementById('<%=DropDownList1.ClientID %>'); alert(objControl.selectedIndex) ddlValue=objControl.options[o bjControl.selectedIndex].value; document.getElementById('hfldResult').value = ddlValue; } </script> in code behind Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim tJavaScript As String = "" tJavaScript = "<script language='javascript'>GetDropDownValue();</script>" Page.ClientScript.RegisterStartupScript(Me.GetType, "abc", tJavaScript) End Sub

              A Offline
              A Offline
              Abhijit Jana
              wrote on last edited by
              #6

              I really don't understand what you are doing.:confused:

              lakshmichawala wrote:

              var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt);

              Where you actual create the DrodownList list? Sometimes you are get taking

              document.getElementById("DropDownList1")

              and some time

              document.getElementById('<%=DropDownList1.ClientID %>');

              Please be specific what is your actual requirement is. As per my understanding You have a Dropdownlist ( Server Side Control ) you add value in the list from JavaScript. Now you want to access the value from Code behind itself. Correct me if I am wrong.

              cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net My Latest Article : IIS Remote Debugging

              1 Reply Last reply
              0
              • L lakshmichawala

                in javascript ============= <script type="text/javascript" language ="javascript" > function AddItem() { var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="One"; opt.value="One" var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="Two"; opt.value="Two" var opt = document.createElement("option"); document.getElementById("DropDownList1").options.add(opt); opt.text="Three"; opt.value="Three"; return false; } function GetDropDownValue() { alert("h") var ddlValue; var objControl = document.getElementById('<%=DropDownList1.ClientID %>'); alert(objControl.selectedIndex) ddlValue=objControl.options[o bjControl.selectedIndex].value; document.getElementById('hfldResult').value = ddlValue; } </script> in code behind Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim tJavaScript As String = "" tJavaScript = "<script language='javascript'>GetDropDownValue();</script>" Page.ClientScript.RegisterStartupScript(Me.GetType, "abc", tJavaScript) End Sub

                B Offline
                B Offline
                Brij
                wrote on last edited by
                #7

                lakshmichawala wrote:

                document.getElementById("DropDownList1").options.add(opt);

                access your dropdown in javascript document.getElementById("<%=DropDownList1.ClientID %>").options.add(opt); I dont think that your value is getting updated in dropdown

                Cheers!! Brij

                L 1 Reply Last reply
                0
                • B Brij

                  lakshmichawala wrote:

                  document.getElementById("DropDownList1").options.add(opt);

                  access your dropdown in javascript document.getElementById("<%=DropDownList1.ClientID %>").options.add(opt); I dont think that your value is getting updated in dropdown

                  Cheers!! Brij

                  L Offline
                  L Offline
                  lakshmichawala
                  wrote on last edited by
                  #8

                  tq i hv solved my problem using request.form in my code behind eg: request.form("dropdownlist1")

                  B 1 Reply Last reply
                  0
                  • L lakshmichawala

                    tq i hv solved my problem using request.form in my code behind eg: request.form("dropdownlist1")

                    B Offline
                    B Offline
                    Brij
                    wrote on last edited by
                    #9

                    Great:thumbsup:

                    Cheers!! Brij

                    1 Reply Last reply
                    0
                    • L lakshmichawala

                      ya in hidden field iam giving the ddl selected value but iam getting null while accessing the value

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      Are you trying to make it impossible for people to help you ? How about posting the code you use to access the value, the script for the control, etc. Why are you not using the client ID from the server to access the hidden field, but you do for the drop down list ? Have you inserts js code to make sure the right control is being found and it's value set ?

                      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                      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