javascript 没有回发的级联下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3449832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:12:32  来源:igfitidea点击:

cascading dropdownlist without postback

c#javascriptasp.netdrop-down-menu

提问by Anchit

So, here's the scenario:

所以,这是场景:

I have a .aspx page having two dropdowns, the data to be loaded in them is usually static(but may change everyday), the first dropdown always displays the main categories and the second dropdown is initially empty.

我有一个 .aspx 页面有两个下拉列表,要加载到其中的数据通常是静态的(但可能每天都在变化),第一个下拉列表始终显示主要类别,第二个下拉列表最初是空的。

Now, the requirement is:

现在,要求是:

As the user selects a category from the first dropdownlist, the second dropdownlist should populate based on the selection just made.

当用户从第一个下拉列表中选择一个类别时,第二个下拉列表应根据刚刚所做的选择填充。

The road-block:

路障:

This should be done without a postback.
My boss wants something like the drop downs on thispage.

这应该在没有回发的情况下完成。
我的老板想要像这个页面上的下拉菜单。

So what I gather is, I'd have to retrieve the data from the database on page load event, store it in the cache(or somewhere else?), then use javascript to handle the selectedindexchanged event of the 1st dropdown, use the data from database stored at pageload and populate the second dropdown according to the selected value of first dropdown.

所以我收集的是,我必须在页面加载事件中从数据库中检索数据,将其存储在缓存中(或其他地方?),然后使用 javascript 处理第一个下拉列表的 selectedindexchanged 事件,使用数据从存储在页面加载的数据库中,并根据第一个下拉列表的选定值填充第二个下拉列表。

It's been 2 days since I'm stuck on this and can't seem to find a feasible solution, and in need of great help.

自从我被困在这个问题上已经 2 天了,似乎找不到可行的解决方案,需要很大的帮助。

Thanks in advance for throwing any light on the matter.

预先感谢您对此事的任何了解。

P.S.- I'm a beginner ASP.Net C# programmer and have no knowledge of Javascript.

PS- 我是 ASP.Net C# 初学者,对 Javascript 一无所知。

回答by Kris van der Mast

Microsoft released the Ajax Control Toolkitwhich has a Cascading dropdownlist control.

Microsoft 发布了Ajax Control Toolkit,它具有Cascading dropdownlist 控件

Edit: to boost performance on retrieving the data I suggest that you make use of the Cacheobject in ASP.NET and cache the data as close to the place as possible where you need it to give it a boost.

编辑:为了提高检索数据的性能,我建议您使用ASP.NET中的Cache对象并将数据缓存在尽可能靠近您需要它的位置以提高性能。

Another trick you can do is to send all the information as a json object when you render the page the first time so you don't need to use webservice calls but simply use javascript parsing to read out the json object and extract the information needed from it to fill up your second dropdownlist.

您可以做的另一个技巧是在第一次呈现页面时将所有信息作为 json 对象发送,这样您就不需要使用 webservice 调用,而只需使用 javascript 解析来读取 json 对象并从中提取所需的信息它来填充你的第二个下拉列表。

回答by David

The hard way: Truly do it client-side. There's a sample hereto get you started.

困难的方法:真正做到客户端。有一个样品在这里让你开始。

The easy way.. Use an Ajax UpdatePanel and do it server-side, but the UpdatePanel will make it not post do a full post-back. (Tutorial here)

简单的方法.. 使用 Ajax UpdatePanel 并在服务器端执行此操作,但 UpdatePanel 将使其不进行完整的回发。(教程在这里

The UpdatePanel will be WAY easier and much quicker to code.

UpdatePanel 将更容易和更快地编写代码。