简体   繁体   中英

Server-side data available client-side

What I want to do is make a dropdownlist choices change depending on other dropdownlists on the page. What I did is write all the possibilities in hidden textboxes. This works, but when there are a lot of choices, the page is slow.

Is there a better way to do this?

Thank you for your time

The best way to do this is with AJAX. In summary you will need to:

  1. write a server side script (ashx probably) that returns the relevant results for a drop down dependent on some variable
  2. use JavaScript (or a library like jQuery) to do the AJAX call to the ashx page. This call will need to pass the defining variable to the server via POST or GET.
  3. attach this AJAX call to the change event of the select boxes so that when they are changed the other ones can be updated
  4. when the AJAX call is completed you will need a JavaScript function that sorts out the returned data. This data could be simple HTML that is added to the page or a more complex JSON object that needs to be formatted. The choice is yours. But this formatting and response needs to be written in JavaScript.

It can be done using some of the available ASP.Net AJAX techniques. If you place the dropdown listboxes in an UpdatePanel and set them to asynchronous postback, you can make them appear to refresh on selection.

Because only the code inside the UpdatePanel is refreshed, the results are loaded much faster.

See this article for a nice example using the updatepanel and a couple of dropdown lists.

As laurencek says AJAX would be end solution for this. On asp.net , there is a good sample provided with a understandable explanation, here . A little use of Web Service that might cause some pause but still a good explanation on what you are looking for.

I'll present one possible scenario.

  1. When your page is first loaded, you display a "default" list of choices. This is done using server side logic.
  2. When a user click on a dropdown list, using Javascript on the client side, you calculate how others should behave and you manipulate them accordingly.
  3. If one of your lists require new information that isn't available on client side, you use AJAX to poll the server for that information.
  4. (optional) you can cache new information on the client side if you don't want the server to be polled every time your users click around on dropdown lists.

AJAX is the way to go as indicated by all others, you can save yourself time by using code that others already written, for example: http://www.codeproject.com/KB/custom-controls/ajaxdropdownlist.aspx

This one is pretty old by now but can at least show you what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM