Server-side data available client-side

删除回忆录丶 提交于 2020-01-06 19:51:35

问题


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


回答1:


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.



回答2:


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.




回答3:


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.




回答4:


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.



回答5:


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.



来源:https://stackoverflow.com/questions/4077783/server-side-data-available-client-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!