“未使用”的绑定 DropDownList GridView 的 EditItemTemplate 中的项目加上当前行项目?

发布于 2024-08-21 10:37:45 字数 1454 浏览 1 评论 0原文

我有 2 个具有可以相互映射的唯一 ID 的表(其中一个表来自我的应用程序外部的数据库,但包含有关我正在跟踪的相同实体的信息)。我想提供一个 UI,用于将“我的”记录与“其他”记录链接起来,这样我就可以将“其他”ID 存储在我的表中以获取匹配记录。简而言之,关键的 UI 元素是一个下拉列表,显示“其他”表中的可用(即尚未链接)记录。

我有一个网格视图,用于在每一行中显示“我的”记录,再加上一列,显示“其他”表中链接记录的一些信息(如果有)。数据是关于建筑物和房产的,所以它可能看起来像这样:

Building1   Dallas   TX    Building1_Dallas_TX
Building2   Memphis  TN    Bldg2_Memphis_TN
Building3   Denver   CO
Building4   Seattle  WA
Building5   Boston   MA    Building5_Boston_MA

其中前 3 列显示“我的”表中的一些信息,最后一列显示“其他”表中的匹配记录,其中记录已经已链接,否则为空白,表示尚未建立链接。我的实际 gridview 还有“我的”表中的几列,但这足以说明需求。

当单击一行的“编辑”(未显示)时,我希望能够编辑所有 4 列。前 3 个是文本框;最后一列应该是一个下拉列表,显示“其他”表中的所有未链接记录,加上“未分配”选项(作为列表中的第一项),以便记录可以“取消链接”或简单地保留未分配状态,加上(对于那些已建立链接的行)链接的项目(它已“使用”,但需要在那里,以便它们可以继续使用它)。

我可以在 gridview RowEditing 事件的代码隐藏中正确填充下拉列表(除了我在 .aspx 中将 AppendDataBoundItems 选项设置为 true 添加为 ListItem 的未分配项),并且我可以在 ddl 的 DataBound 上设置正确的 SelectedValue事件,但在回发时,当我单击更新行时,ddl 中不再有任何项目(除了未分配的项目),因此链接始终被清除。如果我通过直接编辑表来手动设置链接,则进入编辑模式时一切看起来都很好 - ddl 中包含正确的项目,并且它预先选择了正确的项目,但在更新回发时所有内容都会丢失。我已经尝试在页面周期的各个阶段重新绑定 ddl,但显然我还不太了解该周期,因为我无法弄清楚如何让用户选择的项目来更新我的表。

我还尝试使用 SqlDataSource SelectCommand 填充 ddl,但没有运气包括其他数据库中当前链接记录的 ID(即使它在“我的”表中可用,并且我将其设置为 GridView DataKey 并添加它作为 SqlDataSource 的 SelectParameters 中的 ControlParameter。如果没有 ddl 项列表中当前链接的记录,我将

暂时推迟发送代码示例 - 此消息已经太长了。 希望解决方案从我所描述的内容中显而易见......我不认为我的目标很奇怪,但我愿意接受其他方面的说服

! -

I have 2 tables with unique IDs that can be mapped to each other (one table comes from a database outside of my application but has information about the same entities I'm tracking). I want to provide a UI for linking "my" records with the "other" ones, so I can store the "other" IDs in my table for the matching records. In a nutshell, the key UI element is a dropdownlist showing the available (ie, not-yet-linked) records from the "other" table.

I have a gridview for displaying "my" records in each row, plus a column showing some info from the linked record in the "other" table, if any. The data are about buildings and properties, so it might look like this:

Building1   Dallas   TX    Building1_Dallas_TX
Building2   Memphis  TN    Bldg2_Memphis_TN
Building3   Denver   CO
Building4   Seattle  WA
Building5   Boston   MA    Building5_Boston_MA

where the first 3 columns show some information from "my" table, and the last one shows the matching record from the "other" table, where the records have already been linked, else a blank where no link has been established. My actual gridview has several more columns from "my" table, but this is enough to illustrate the need.

When Edit (not shown) is clicked for a row, I want to be able to edit all 4 columns. The first 3 are textboxes; the last column should be a dropdownlist showing all of the unlinked records from the "other" table, plus an "Unassigned" option (as the first item in the list) so records can be "unlinked" or simply left unassigned, plus (for those rows that already have a link established) the linked item (it's "used" but it needs to be in there so they can continue to use it).

I can populate the dropdownlist correctly in code-behind in the gridview RowEditing event (except for the Unassigned item which I add as a ListItem in .aspx with the AppendDataBoundItems option set to true), and I can set the correct SelectedValue on the ddl's DataBound event, but on postback when I click to Update the row, the ddl has no items in it anymore (except for the Unassigned item) so the link is always cleared. If I set a link manually by directly editing my table, everything looks fine going into Edit mode -- the ddl has the correct items in it and it pre-selects the correct item, but all is lost on the Update postback. I have tried re-binding the ddl at various stages of the page cycle but clearly I don't quite understand that cycle well enough yet because I cannot figure out how to get the user-selected item to update my table.

I've also tried populating the ddl with a SqlDataSource SelectCommand, but have had no luck including the ID of currently-linked records in the other database (even though it's available in "my" table and I set it as a GridView DataKey and add it as a ControlParameter in the SelectParameters for the SqlDataSource. Without the currently-linked record in the ddl item list, I can't keep existing links.

I'll hold off sending a code sample for now -- this message is already too long! -- in hopes that the solution is obvious from what I've described... I don't think my aim is weird, but I'm open to being persuaded otherwise.

Many thanks in advance!

Chris

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

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

发布评论

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

评论(1

心房的律动 2024-08-28 10:37:45

好吧,我不会详细介绍代码,因为也许您已经完成了我要建议的操作。

对于最后一列(下拉列表),您必须执行 SQL 语句来获取未链接的项目。使用“WHERE xx NOT IN”。对于“未分配”选项,只需将一个项目添加到与上面相同的代码范围内的列表中即可。

下一步是手动处理 GridView 的编辑事件。插入、更新和删除。

我想这样你就不会有问题了。

Ok, I'm not going into details with code because maybe you already done what I'm about to suggest.

For the last column, the drop down list, you have to execute an SQL statement to grab you the unlinked items. Use the "WHERE xx NOT IN". For the "Unassigned" option, just add an item to the list in the same code scope as above.

Next step is to handle the GridView's editing events manually. Insert, Update and Delete.

I think that way you won't have problems.

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