访问 ListView 编辑命令上的控件

发布于 2024-12-28 21:19:51 字数 1673 浏览 5 评论 0原文

在我的 ListView 中,我有一个 ItemTemplate 和 EditItemTemplate ,分别看起来像这样。

在此处输入图像描述 -------> 在此处输入图像描述

当我单击“编辑”按钮时,它切换到 EditItemTemplate在右侧视图中,我想预填充 Textbox 并在 DropDownList 中选择相应的 option。我该怎么做?

在您说使用类似以下内容之前,请知道我已经探索了我能想到的所有可能的变体。抱歉要求如此之高,但如果您回答,请准备好引导我完成这一过程。 ^.^ 我在这个问题上已经被困了好几个月了:(

Dim lv As ListView = DirectCast(sender, ListView) 'sender is the ListView on the ItemCommand event
Dim ddl As DropDownList = DirectCast(lv.Items(0).FindControl("NewProductName_ddl"), DropDownList)
Dim tb As TextBox = DirectCast(lv.Items(0).FindControl("NewProductName_tb"), TextBox)

更新 - RAWR!!

哦,我的天哪,这么近,但没有雪茄。下面的代码适用于只有一件物品时的预填充位于 ListView 中,但是当存在多个项目时,它会抛出 NullReferenceException :(

'PROBLEM WAS HERE: Compare to the working code in my answer.
Protected Sub NewProduct_ItemDataBound(ByVal sender As ListView, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
    If sender.EditIndex > -1 Then
        Dim ddl As DropDownList = DirectCast(e.Item.FindControl("NewProductName_ddl"), DropDownList)
        Dim tb As TextBox = DirectCast(e.Item.FindControl("NewProductName_cb"), TextBox)

        ddl.Items.FindByValue(sender.DataKeys(sender.EditIndex)("ID").ToString).Selected = True 'Prefills the DropDownList
        tb.Text = sender.DataKeys(sender.EditIndex)("Product").ToString 'Prefills the TextBox
    End If
End Sub

In my ListView I have an ItemTemplate and EditItemTemplate which look something like this, respectively.

enter image description here -------> enter image description here

When I click the "Edit" button, and it switches to the EditItemTemplate view on the right, I want to prefill the Textbox and select the corresponding option in the DropDownList. How can I do this?

Before you say to use something like the following, please know that I've already explored every possible variation I can think of. Sorry to be so demanding, but please be prepared to walk me through this one if you answer. ^.^ I've been stuck on this issue for literally months :(

Dim lv As ListView = DirectCast(sender, ListView) 'sender is the ListView on the ItemCommand event
Dim ddl As DropDownList = DirectCast(lv.Items(0).FindControl("NewProductName_ddl"), DropDownList)
Dim tb As TextBox = DirectCast(lv.Items(0).FindControl("NewProductName_tb"), TextBox)

UPDATE - RAWR!!

Oh my freaking goodness, SO CLOSE, but no cigar. The following code worked for prefilling when only one item was in the ListView, but when more than one items exist, it throws a NullReferenceException :(

'PROBLEM WAS HERE: Compare to the working code in my answer.
Protected Sub NewProduct_ItemDataBound(ByVal sender As ListView, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
    If sender.EditIndex > -1 Then
        Dim ddl As DropDownList = DirectCast(e.Item.FindControl("NewProductName_ddl"), DropDownList)
        Dim tb As TextBox = DirectCast(e.Item.FindControl("NewProductName_cb"), TextBox)

        ddl.Items.FindByValue(sender.DataKeys(sender.EditIndex)("ID").ToString).Selected = True 'Prefills the DropDownList
        tb.Text = sender.DataKeys(sender.EditIndex)("Product").ToString 'Prefills the TextBox
    End If
End Sub

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

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

发布评论

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

评论(2

初相遇 2025-01-04 21:19:51

尤里卡!!

我高兴得超乎想象!!全部大写,也没有粗体来表达我现在有多快乐:)

首先我想给 这个问题让我指明了正确的方向。现在来看答案,这是我发现的上述链接中提供的答案的最理想变体:

ItemDataBound 事件是关键,但重要的是要注意,此事件将为每个项目触发存在于您的 ListView 中,因此,您必须小心您的方法。这里有两个对我同样有效的选择。

选项1 - 最优雅;仅对有问题的项目而不是所有项目运行 FindControl。

Protected Sub NewProduct_ItemDataBound(ByVal sender As ListView, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
    Dim i As Integer = sender.EditIndex
    If i = e.Item.DataItemIndex Then
        Dim ddl As DropDownList = DirectCast(e.Item.FindControl("NewProductName_ddl"), DropDownList)
        Dim tb As TextBox = DirectCast(e.Item.FindControl("NewProductName_cb"), TextBox)

        ddl.Items.FindByValue(sender.DataKeys(i)("ID").ToString).Selected = True 'Prefills the DropDownList
        tb.Text = sender.DataKeys(i)("Product").ToString 'Prefills the TextBox
    End If
End Sub

选项 2 - 基于引用的问题,但进行了关键检查以确保对象非空。

Protected Sub NewProduct_ItemDataBound(ByVal sender As ListView, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
    Dim i As Integer = sender.EditIndex
    If i > -1 Then
        Dim ddl As DropDownList = DirectCast(e.Item.FindControl("NewProductName_ddl"), DropDownList)
        Dim tb As TextBox = DirectCast(e.Item.FindControl("NewProductName_cb"), TextBox)

        If Not IsNothing(ddl) Then
            ddl.Items.FindByValue(sender.DataKeys(i)("ID").ToString).Selected = True 'Prefills the DropDownList
        End If
        If Not IsNothing(tb) Then
            tb.Text = sender.DataKeys(i)("Product").ToString 'Prefills the TextBox
        End If
    End If
End Sub

我稍后可能会对这个答案进行改进,但这对我来说很有效。 :)

EUREKA!!

I am elated beyond imagination!! All caps, nor bold do justice to how happy I am right now :)

First I wanna give props to this question which got me pointed in the right direction. Now onto the answer, which is the most ideal variation I have found of the answer provided in the above link:

The ItemDataBound event is the key, but it's important to note that this event will fire for each item that exists in your ListView and for that reason, you must be careful in your approach. Here are two options that worked equally well for me.

Option 1 - Most elegant; only runs FindControl on the item in question rather than all items.

Protected Sub NewProduct_ItemDataBound(ByVal sender As ListView, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
    Dim i As Integer = sender.EditIndex
    If i = e.Item.DataItemIndex Then
        Dim ddl As DropDownList = DirectCast(e.Item.FindControl("NewProductName_ddl"), DropDownList)
        Dim tb As TextBox = DirectCast(e.Item.FindControl("NewProductName_cb"), TextBox)

        ddl.Items.FindByValue(sender.DataKeys(i)("ID").ToString).Selected = True 'Prefills the DropDownList
        tb.Text = sender.DataKeys(i)("Product").ToString 'Prefills the TextBox
    End If
End Sub

Option 2 - Based on the referenced question, but with a crucial check to ensure non-null object.

Protected Sub NewProduct_ItemDataBound(ByVal sender As ListView, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
    Dim i As Integer = sender.EditIndex
    If i > -1 Then
        Dim ddl As DropDownList = DirectCast(e.Item.FindControl("NewProductName_ddl"), DropDownList)
        Dim tb As TextBox = DirectCast(e.Item.FindControl("NewProductName_cb"), TextBox)

        If Not IsNothing(ddl) Then
            ddl.Items.FindByValue(sender.DataKeys(i)("ID").ToString).Selected = True 'Prefills the DropDownList
        End If
        If Not IsNothing(tb) Then
            tb.Text = sender.DataKeys(i)("Product").ToString 'Prefills the TextBox
        End If
    End If
End Sub

I may make improvements to this answer later, but this did the trick for me. :)

秉烛思 2025-01-04 21:19:51

很棒的帖子!我遇到了同样的问题,你节省了我数小时的尝试和错误。只是想指出,当将第一个选项与 .NET Framework 3.5 或更低版本一起使用时,DataItemIndex 不可用。要解决这个问题,您可以替换

If i = e.Item.DataItemIndex Then

If i = DirectCast(e.Item, IDataItemContainer).DataItemIndex Then

Great post! I had the same problem and you saved me hours of trial and error. Just wanted to point out that when using your first option with .NET Framework 3.5 or lower, DataItemIndex isn't available. To work around it you can replace

If i = e.Item.DataItemIndex Then

With

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