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. Get value from javascript function into Dropdown and Display selected Value

Get value from javascript function into Dropdown and Display selected Value

Scheduled Pinned Locked Moved ASP.NET
javascripthelp
5 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.
  • N Offline
    N Offline
    NET India
    wrote on last edited by
    #1

    Hello Friends, I've two dropdownlist(ddlCountry,ddlState). Onchange event of CountryDropdown(ddlCountry) StateNames are displayed into StateName dropdown using javascript. And it's working fine but prooblem is if i print the value of selected StateName on a button click at code-behind i got an error <b>Object reference set to an instance of an object</b>. So can anybody let me know what should i do know

    P 1 Reply Last reply
    0
    • N NET India

      Hello Friends, I've two dropdownlist(ddlCountry,ddlState). Onchange event of CountryDropdown(ddlCountry) StateNames are displayed into StateName dropdown using javascript. And it's working fine but prooblem is if i print the value of selected StateName on a button click at code-behind i got an error <b>Object reference set to an instance of an object</b>. So can anybody let me know what should i do know

      P Offline
      P Offline
      padmanabhan N
      wrote on last edited by
      #2

      your question is clear... Plzzzz send the code upto which level you have tried and the line where u r getting that error...

      Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]

      N 1 Reply Last reply
      0
      • P padmanabhan N

        your question is clear... Plzzzz send the code upto which level you have tried and the line where u r getting that error...

        Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]

        N Offline
        N Offline
        NET India
        wrote on last edited by
        #3

        This is my javascript function function myFunc() { if(document.getElementById("ddlCountry").value=="India") { //Remove Existing Values var e=document.getElementById("ddlState"); while(e.firstChild) { e.removeChild(e.firstChild); } var myArray=new Array(2); myArray[0]="Delhi"; myArray[1]="Mumbai"; for(var i=0;i<myArray.length;i++) { //Option(Text,Value) e.options[i]=new Option(myArray[i]); } } else if(document.getElementById("ddlCountry").value=="America") { //Remove Existing Values var e=document.getElementById("ddlState"); while(e.firstChild) { e.removeChild(e.firstChild); } var myArray=new Array(2); myArray[0]="Washington"; myArray[1]="LA"; for(var i=0;i<myArray.length;i++) { //Option(Text,Value) e.options[i]=new Option(myArray[i]); } } } This is my .aspx code <asp:DropDownList ID="ddlCountry" runat="server" onchange="myFunc(this)"> <asp:ListItem Text="Select Country" Value="Select Country"></asp:ListItem> <asp:ListItem Text="India" Value="India"></asp:ListItem> <asp:ListItem Text="America" Value="America"></asp:ListItem> </asp:DropDownList> <asp:DropDownList ID="ddlState" runat="server"> </asp:DropDownList> This is my Code-Behind(on button click event) code string name = ddlCountry.SelectedItem.Text+","+ddlState.SelectedItem.Text; Label1.Text = name.ToString(); And

        P 1 Reply Last reply
        0
        • N NET India

          This is my javascript function function myFunc() { if(document.getElementById("ddlCountry").value=="India") { //Remove Existing Values var e=document.getElementById("ddlState"); while(e.firstChild) { e.removeChild(e.firstChild); } var myArray=new Array(2); myArray[0]="Delhi"; myArray[1]="Mumbai"; for(var i=0;i<myArray.length;i++) { //Option(Text,Value) e.options[i]=new Option(myArray[i]); } } else if(document.getElementById("ddlCountry").value=="America") { //Remove Existing Values var e=document.getElementById("ddlState"); while(e.firstChild) { e.removeChild(e.firstChild); } var myArray=new Array(2); myArray[0]="Washington"; myArray[1]="LA"; for(var i=0;i<myArray.length;i++) { //Option(Text,Value) e.options[i]=new Option(myArray[i]); } } } This is my .aspx code <asp:DropDownList ID="ddlCountry" runat="server" onchange="myFunc(this)"> <asp:ListItem Text="Select Country" Value="Select Country"></asp:ListItem> <asp:ListItem Text="India" Value="India"></asp:ListItem> <asp:ListItem Text="America" Value="America"></asp:ListItem> </asp:DropDownList> <asp:DropDownList ID="ddlState" runat="server"> </asp:DropDownList> This is my Code-Behind(on button click event) code string name = ddlCountry.SelectedItem.Text+","+ddlState.SelectedItem.Text; Label1.Text = name.ToString(); And

          P Offline
          P Offline
          padmanabhan N
          wrote on last edited by
          #4

          i too got the same error from your code... The problem is due to postback. The ddlCountry value is maintained but ddlState value is not maintained. So, the error appears. Try to maintain the value for the ddlState too...

          Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]

          N 1 Reply Last reply
          0
          • P padmanabhan N

            i too got the same error from your code... The problem is due to postback. The ddlCountry value is maintained but ddlState value is not maintained. So, the error appears. Try to maintain the value for the ddlState too...

            Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]

            N Offline
            N Offline
            nitin_vatsus
            wrote on last edited by
            #5

            Save the state of drop down list get value from request.form or query string do not fill all entry every time because if you did so every time new values is filled and state can not be maintained you can try one more option before submitting the form save the value into hidden variable by using java script and then get values from request object not from drop down because it lost their value when it fill again

            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