访问 ListView 编辑命令上的控件
在我的 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.
------->
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尤里卡!!
我高兴得超乎想象!!全部大写,也没有粗体来表达我现在有多快乐:)
首先我想给 这个问题让我指明了正确的方向。现在来看答案,这是我发现的上述链接中提供的答案的最理想变体:
ItemDataBound
事件是关键,但重要的是要注意,此事件将为每个项目触发存在于您的ListView
中,因此,您必须小心您的方法。这里有两个对我同样有效的选择。选项1 - 最优雅;仅对有问题的项目而不是所有项目运行 FindControl。
选项 2 - 基于引用的问题,但进行了关键检查以确保对象非空。
我稍后可能会对这个答案进行改进,但这对我来说很有效。 :)
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 yourListView
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.
Option 2 - Based on the referenced question, but with a crucial check to ensure non-null object.
I may make improvements to this answer later, but this did the trick for me. :)
很棒的帖子!我遇到了同样的问题,你节省了我数小时的尝试和错误。只是想指出,当将第一个选项与 .NET Framework 3.5 或更低版本一起使用时,DataItemIndex 不可用。要解决这个问题,您可以替换
为
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
With