Load DropDownList from client side javascript - can it be done?

Discussion in 'ASP .Net Web Controls' started by James Radke, Nov 7, 2003.

  1. James Radke

    James Radke Guest

    Hello,

    I have a vb.net aspx page that has a lot of controls on it. To improve
    performance and eliminate the number of page refreshes due to postbacks I
    have started using a scheme where I call client side javascript functions,
    then using javascript call various WebMethod (i.e. webservices) in my
    application.

    This has dramatically increased the performance of the web page. However, I
    have one issue: there is one spot where I dynamically populate a listbox
    control on my web form (currently it is databound), and it starts with it's
    visibility set to false.

    Is there a way, via client-side javascript, that I can re-populate this
    databound drop down list and display it based on values returned from my
    WebMethod?

    I would want the list to stay populated even if the page does a refresh....

    Any help would be appreciated!

    Thanks!

    Jim
     
    James Radke, Nov 7, 2003
    #1
    1. Advertising

  2. Hi Jim,

    The ListBox becomes a <select> tag on the browser. Documentation on the
    Select tag can be found at:
    http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/select.asp

    Also, the HTML and DHTML Reference can be found at:
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_en
    try.asp


    1) Removing items

    The quick way: Form1.ListBox1.innerHTML = "";
    Or remove individual lines. That lets you be selective about removing items.
    if (Form1.ListBox1.length > 0)
    {
    var n1, n2;
    n1=Form1.ListBox1.firstChild
    n2=Form1.ListBox1.lastChild
    while (n1 != n2)
    {Form1.ListBox1.removeChild(n2); n2=Form1.ListBox1.lastChild;}
    Form1.ListBox1.removeChild(n1)
    }


    2) Adding items

    var oOption = document.createElement("OPTION");
    Form1.ListBox1.options.add(oOption);
    oOption.innerText = "Two";
    oOption.value = "2";


    3) Persisting through a postback

    When you post a form, included Select tags do not post all their data.
    Therefore, you will need to add your data to a hidden field so that the
    server will receive it. I suggest a HTML Hidden field with runat=server. As
    you add items to the Select list, also add them to this hidden field using
    delimiters of your choice. On the server, you will need to populate the
    ListBox from this data.


    Thank you, Mike
    Microsoft, ASP.NET Support Professional

    Microsoft highly recommends to all of our customers that they visit the
    http://www.microsoft.com/protect site and perform the three straightforward
    steps listed to improve your computer's security.

    This posting is provided "AS IS", with no warranties, and confers no rights.


    --------------------
    > Reply-To: "James Radke" <>
    > From: "James Radke" <>
    > Subject: Load DropDownList from client side javascript - can it be done?
    > Date: Fri, 7 Nov 2003 13:11:38 -0600
    > Lines: 26
    > X-Priority: 3
    > X-MSMail-Priority: Normal
    > X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    > X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    > Message-ID: <>
    > Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    > NNTP-Posting-Host: cpe-24-167-241-101.wi.rr.com 24.167.241.101
    > Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
    > Xref: cpmsftngxa06.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webcontrols:16005
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    >
    > Hello,
    >
    > I have a vb.net aspx page that has a lot of controls on it. To improve
    > performance and eliminate the number of page refreshes due to postbacks I
    > have started using a scheme where I call client side javascript functions,
    > then using javascript call various WebMethod (i.e. webservices) in my
    > application.
    >
    > This has dramatically increased the performance of the web page.

    However, I
    > have one issue: there is one spot where I dynamically populate a listbox
    > control on my web form (currently it is databound), and it starts with

    it's
    > visibility set to false.
    >
    > Is there a way, via client-side javascript, that I can re-populate this
    > databound drop down list and display it based on values returned from my
    > WebMethod?
    >
    > I would want the list to stay populated even if the page does a

    refresh....
    >
    > Any help would be appreciated!
    >
    > Thanks!
    >
    > Jim
    >
    >
    >
     
    Mike Moore [MSFT], Nov 7, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    2
    Views:
    1,087
    Craig Deelsnyder
    Jul 12, 2004
  2. Boss302
    Replies:
    0
    Views:
    1,070
    Boss302
    Nov 21, 2006
  3. Bogdan
    Replies:
    2
    Views:
    670
    Bogdan
    Jun 9, 2008
  4. Blair Taylor
    Replies:
    1
    Views:
    349
    Alvin Bruney
    Aug 7, 2003
  5. Zhidian Du
    Replies:
    0
    Views:
    168
    Zhidian Du
    Feb 21, 2004
Loading...

Share This Page