How to add the values to listbox control using javascript

Last post 07-09-2008 11:14 AM by NC01. 5 replies.

Sort Posts:

  • How to add the values to listbox control using javascript

    07-09-2008, 2:43 AM
    • Member
      point Member
    • Jilani.sak
    • Member since 04-19-2008, 8:43 AM
    • Posts 29

    I want to add the items to listbox control using javascript function. How can I achieve this?

  • Re: How to add the values to listbox control using javascript

    07-09-2008, 3:49 AM
    • Contributor
      5,079 point Contributor
    • NHOQUE
    • Member since 04-02-2008, 9:00 AM
    • Kumamoto, Japan
    • Posts 849

    Try this,

      

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>ListBox Example</title>
        <script type="text/javascript">
        function defacto(){
        //document.getElementById("lblListBox").value="three";
        var o= document.createElement("option");
        o.label = "Cycle"
        o.value = "four";
        document.getElementById("lblListBox").add(o);
        }
        </script>
    </head>
    <body onload="defacto()">
        <form id="form1" runat="server">
        <div>
        <select id="lblListBox">
            <option label="Car" value="one" ></option>
            <option label="Bus" value="two"></option>
            <option label="Bike" value="three"></option>
        </select>
        </div>
        </form>
    </body>
    </html>
    
     

    if it helps you, please mark it as answer.

    Thanks.

    HOQUE MD.NAZMUL
    [document.getReaders]
  • Re: How to add the values to listbox control using javascript

    07-09-2008, 4:35 AM
    • Contributor
      3,091 point Contributor
    • sameer_khanjit
    • Member since 12-10-2007, 1:06 PM
    • Indore India
    • Posts 638


    var objsel = document.getElementById("lb_updselitems");

    var customOptions;
    customOptions = document.createElement("OPTION");
    customOptions.text = "sam";
    customOptions.value = "sam1";
    objsel.add(customOptions,0);

    We Are Looking for .NET/PHP Projects

    Contact Details :-

     Email - sameer.khanjit@gmail.com

     Mobile no. : +91-9893795983

     View Blog

    linkedin Asp.net Group

    Don't forget to click “Mark as Answer” on the post that helped you
  • Re: How to add the values to listbox control using javascript

    07-09-2008, 4:43 AM
    • Member
      point Member
    • Jilani.sak
    • Member since 04-19-2008, 8:43 AM
    • Posts 29

    Thanks for your reply, I have try above code, it work fine, But one problem is the values are not visible in listbox

    I have written below code, it work fine

    var lstDate = document.getElementById('ctl00_ContentPlaceHolder1_lstDates1');

    var newoption = new Option(window.CP_targetInput.value,window.CP_targetInput.value);

    lstDate.options.add(newoption);

    Now, I want to store these values (listbox values) into session. how can I achieve this?

     

  • Re: How to add the values to listbox control using javascript

    07-09-2008, 4:54 AM
    • Contributor
      3,091 point Contributor
    • sameer_khanjit
    • Member since 12-10-2007, 1:06 PM
    • Indore India
    • Posts 638

     first iof all try hard coded valus like ("sam1","sam2") etc then get values from elements using  getlementbyid then assign value in option


    var a;
    a = <%=Session("name")%>;

    But can not do  Something like:

    var a;
    a = "test";
    <%Session("name")=%> = a; ??

    this is not available in javascript... 

    We Are Looking for .NET/PHP Projects

    Contact Details :-

     Email - sameer.khanjit@gmail.com

     Mobile no. : +91-9893795983

     View Blog

    linkedin Asp.net Group

    Don't forget to click “Mark as Answer” on the post that helped you
  • Re: How to add the values to listbox control using javascript

    07-09-2008, 11:14 AM
    Answer
    • All-Star
      75,065 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,955

    I believe that your problem is that when you add an item to a DropDownList client-side (using JavaScript) that when a PostBack occurs, the item will disappear. The only way around this is to add a hidden server-control to the page and when adding the item client-side, add it to the hidden server-control also, so that when a PostBack occurs you can also add it server-side.

    Here's an example.

    aspx file:

    <form id="Form1" method="post" runat="server">
     <asp:DropDownList id="DropDownList1" runat="server">
      <asp:listitem value="1">Item 1</asp:listitem>
      <asp:listitem value="2">Item 2</asp:listitem>
      <asp:listitem value="3">Item 3</asp:listitem>
      <asp:listitem value="4">Item 4</asp:listitem>
      <asp:listitem value="5">Item 5</asp:listitem>
      <asp:listitem value="6">Item 6</asp:listitem>
     </asp:DropDownList>
     <input id="DropDownList1NewItems" type="hidden" name="DropDownList1NewItems" runat="server">
     <input type="button" onclick="addItem();" value="Add Item">
    </form>

    <script type="text/javascript">
    <!--
    function addItem()
    {
     // Add the item here...
     var ddlRef = document.getElementById('<%= DropDownList1.ClientID %>');
     var newOption = window.document.createElement('OPTION');
     newOption.text = 'SomeNewItem';
     newOption.value = 'SomeNewItem';

     ddlRef.options.add(newOption);

     var hiddenElementRef = document.getElementById('<%= DropDownList1NewItems.ClientID %>');

     if ( hiddenElementRef.value.length > 0 )
      hiddenElementRef.value += ';';

     hiddenElementRef.value += 'SomeNewItem';
    }
    // -->
    </script>

    aspx.cs file:

    private void Page_Load(object sender, System.EventArgs e)
    {
     if ( this.IsPostBack )
     {
      if ( DropDownList1NewItems.Value.Length > 0 )
      {
       string [] newItemsArray = DropDownList1NewItems.Value.Split(';');
       for (int i=0; i<newItemsArray.Length; i++)
       {
        string newItem = newItemsArray[i];

        DropDownList1.Items.Add(new ListItem(newItem, newItem));
       }

       DropDownList1NewItems.Value = string.Empty;
      }
     }
    }

    NC...

Page 1 of 1 (6 items)