简体   繁体   English

服务器端数据在客户端可用

[英]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. 最好的方法是使用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 编写一个服务器端脚本(可能是ashx),该脚本返回相关结果以进行依赖于某些变量的下拉列表
  2. use JavaScript (or a library like jQuery) to do the AJAX call to the ashx page. 使用JavaScript(或类似jQuery的库)对ashx页面进行AJAX调用。 This call will need to pass the defining variable to the server via POST or GET. 该调用将需要通过POST或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 将此AJAX调用附加到选择框的change事件,以便在更改它们时可以更新其他框
  4. when the AJAX call is completed you will need a JavaScript function that sorts out the returned data. 完成AJAX调用后,您将需要一个JavaScript函数来整理返回的数据。 This data could be simple HTML that is added to the page or a more complex JSON object that needs to be formatted. 此数据可以是添加到页面的简单HTML,也可以是需要格式化的更复杂的JSON对象。 The choice is yours. 这是你的选择。 But this formatting and response needs to be written in JavaScript. 但是这种格式和响应需要用JavaScript编写。

It can be done using some of the available ASP.Net AJAX techniques. 可以使用一些可用的ASP.Net AJAX技术来完成。 If you place the dropdown listboxes in an UpdatePanel and set them to asynchronous postback, you can make them appear to refresh on selection. 如果将下拉列表框放在UpdatePanel并将其设置为异步回发,则可以使它们显示为在选择时刷新。

Because only the code inside the UpdatePanel is refreshed, the results are loaded much faster. 因为仅刷新UpdatePanel的代码,所以加载结果要快得多。

See this article for a nice example using the updatepanel and a couple of dropdown lists. 有关使用updatepanel和几个下拉列表的漂亮示例,请参见本文

As laurencek says AJAX would be end solution for this. 正如劳伦斯克所说,AJAX将是最终解决方案。 On asp.net , there is a good sample provided with a understandable explanation, here . asp.net中,提供了简单易懂的解释,一个好的样品在这里 A little use of Web Service that might cause some pause but still a good explanation on what you are looking for. 稍微使用Web Service可能会导致一些暂停,但对于要查找的内容仍然有很好的解释。

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. 当用户在客户端上使用Javascript单击下拉列表时,您可以计算其他人的行为并相应地进行操作。
  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. 如果您的列表之一需要客户端无法提供的新信息,则可以使用AJAX轮询服务器以获取该信息。
  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 AJAX是所有其他人所指出的方式,您可以使用其他人已经编写的代码来节省时间,例如: 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. 到目前为止,这已经很老了,但至少可以向您展示您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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