在 ASP.NET 中链接绑定 DropDownList

发布于 2024-10-20 09:17:39 字数 446 浏览 1 评论 0原文

我正在用 ASP.NET 编写一个网站。 我有一个数据列表,显示数据表中的所有书籍。对于每本书,我都有一个按钮可以将项目置于编辑模式。 每个项目都有一系列字段,如标题、作者、关键字等。其中有一个部分和一个部门字段,它们描述了哪个部分以及该部分中的哪个部门拥有该书。

这是我遇到的问题:

部分和部门名称存储在两个数据表中,它们引用哪些部门属于哪个部分。从逻辑上讲,编辑模板有两个下拉列表:一个用于部分,一个用于部门。我希望能够根据第一个列表中选择的部分来限制第二个下拉列表中的部门选择。到目前为止我还好,接下来的事情让我发疯。

如果用户更改了该部分,然后出于某种原因决定将其更改回其原始值,我希望所选部门默认恢复其原始值,以避免它被无意中更改。

我尝试对选定的值进行数据绑定,但是当该部分更改为不包含原始分区的部分时,会出现可怕的“列表中未找到项目”错误。

有谁知道如何实现这种依赖关系? 非常感谢!

I am writing a website in ASP.NET.
I have a DataList which displays all the books in my datatable. For each book I have a button to put the item into edit mode.
Each item has a series of fields, like title, author, keywords etc. Amongst those there is a section and a division field, which describe which section and which division within the section owns the book.

Here is the problem I have:

The section and division names are stored in two datatables which reference which divisions belong to which section. Logically, the editing template has got two dropdownlists: one for the section and one for the division. I want to be able to limit the choice of divisions in the second dropdownlist based on the section selected in the first list. I'm ok up to here, it's the next bit which is driving me crazy.

If the user changes the section and then for some reason decides to change it back to its original value, I would like the selected division to default back to its original value to avoid it being inadvertently changed.

I have tried to databind the selected value, but that gives the horrible "item not found in list" error when the section is changed to one which does not contain the original division.

Does anyone know how this kind of dependency can be implemented?
Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永言不败 2024-10-27 09:17:39

实际上这真的很愚蠢,我只是通过制作一个包含两个下拉列表的用户控件来对其进行排序。有两个属性,我在其中存储下拉列表所需的初始值,然后在每个下拉列表的数据绑定事件中,我只是在其中放置一个 try catch ,我尝试设置列表的选定值,只要下拉列表有效,它就可以很好地工作尚未被禁用。

为了在选择与该项目对应的部分时设置回原始除法值,我只是在页面加载时将该值存储在隐藏字段中。

真是蠢到这种地步了……

Actually it was really stupid, I just sorted it by making a user control containing both dropdownlists. There are two properties in which I store the desired initial values of the dropdownlists, then in the databound event of each dropdownlist I just put a try catch in whcih I attempt to set the selectedvalue of the list, which works nicely as long as the dropdownlist has not been disabled.

In order to set back to the original division value when selecting the section corresponding to the item I just store the value in a hidden field on page load.

As stupid as that really...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文