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

J

James Radke

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
 
M

Mike Moore [MSFT]

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.


--------------------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top