根据另一个值填充下拉列表的正确方法?
我正在处理的一些 ASP.Net 代码遇到了一个小问题。为了简单起见,假设我有两个下拉列表,一个充满各种项目,另一个充满其他项目,但基于第一个下拉列表的选定项目。我目前将变量设置为隐藏输入,以检查第一个下拉列表中的项目是否已更改以及是否必须更改第二个下拉列表,但我还必须检查以确保是否有其他原因导致回发重新填充第二个下拉列表,以免丢失用户当前选择的内容。此检查还允许我确保当我离开页面时,会选择正确的值,而不是被回发重新填充所替换的值。
我是 ASP.Net/HTML/CSS/Javascript 等的新手。我知道虽然我所拥有的方法有效,但它距离正确的做事方式还差得很远。请帮助我纠正我的方法并找出解决当前问题的最佳方法。一些好的 ASP.Net 教程网站也会很好,因为我需要不断提高我的技能。
I'm running into a minor issue with some ASP.Net code I'm working on. For simplicity sake lets say I have two dropdownlists, one that is full of various items and another that is full of other items, but based off the selected item of the first dropdownlist. I currently set variables into hidden inputs to check to see if the item in the first dropdownlist has changed and if it has to change the second dropdownlist, but I'm also having to check to make sure that if something else causes a postback to NOT repopulate the second dropdownlist as to not lose what is currently selected by the user. This check also allows me to make sure that when I navigate away from the page, the correct values are selected and not something replaced by a postback repopulation.
I'm new to ASP.Net/HTML/CSS/Javascript and the like. I know while what I have works, it isn't even close to being the correct way to do things. Please help me correct my ways and figure out the BEST way to solve this current problem. Some good ASP.Net tutorial websites would be nice too as I need to keep improving my skills with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 autopostback true 选定索引更改事件:
并让该事件填充第二个下拉列表:
编辑
好吧,如果您需要它从第一页加载填充,您可以执行以下操作,或者更好地仍然创建一个从页面加载和选定索引更改调用的方法,该方法将填充您的下拉列表。
Try autopostback true with the selected index changed event:
And have the event populate the second dropdown:
EDIT
Well if you need it to populate from first page load you could do the following, or better still make a method which is called from both page load and selected index changed which will populate your dropdown.
您可能想看看
事件
以及DropDownList 类
You probably want to have a look at
Event
and some examples in DropDownList Class