关于访问的ListView编辑命令控制 [英] Accessing Controls on ListView Edit Command
问题描述
在我的的ListView
我有一个的ItemTemplate
和 EditItemTemplate中
这是这个样子,分别为。
------->
当我点击编辑按钮,切换到右侧的 EditItemTemplate中
的看法,我想pre灌注的文本框
,然后选择相应的选项
在的DropDownList
。我怎样才能做到这一点?
在你说要使用类似下面,请知道,我已经探讨各种可能的变化我能想到的。很抱歉说得这么苛刻,但请ppared走我走过这一个,如果你回答$ P $。 ^ ^我一直停留在这个问题上的字面月:(
昏暗LV作为的ListView = DirectCast(发件人,ListView控件)发件人是ItemCommand事件ListView控件
朦胧DDL作为DropDownList的= DirectCast(lv.Items(0).FindControl(NewProductName_ddl),DropDownList的)
昏暗TB作为文本框= DirectCast(lv.Items(0).FindControl(NewProductName_tb),文本框)
更新 - RAWR!
哦,我再用善良,如此接近,但没有雪茄。下面code工作了prefilling时,只有一个项目是在ListView,但是当多个项目存在,它抛出一个的的NullReferenceException 的:(
'的问题是这里:比较在我的回答工作code。
保护小组NewProduct_ItemDataBound(BYVAL发件人为ListView控件,BYVAL E上ListViewItemEventArgs)处理NewProduct.ItemDataBound
如果sender.EditIndex>然后-1
朦胧DDL作为DropDownList的= DirectCast(e.Item.FindControl(NewProductName_ddl),DropDownList的)
昏暗TB作为文本框= DirectCast(e.Item.FindControl(NewProductName_cb),文本框) ddl.Items.FindByValue(sender.DataKeys(sender.EditIndex)(ID)。的ToString).Selected = TRUE'prefills将DropDownList
tb.Text = sender.DataKeys(sender.EditIndex)(产品)。的ToString'prefills文本框
万一
结束小组
EUREKA !!
我心花怒放超乎想象!全部大写,也大胆做正义的我是多么高兴现在:)
首先,我想给道具<一个href=\"http://stackoverflow.com/questions/825048/accessing-controls-in-the-edititemtemplate-of-a-listview\">this问题这让我在正确的方向。现在到了答案,这是我发现在上面的链接提供了答案最理想的变化:
的的ItemDataBound
事件是关键,但要注意,此事件将触发该存在于你的 ListView中的每一项很重要
,因为这个原因,你必须在你的方法小心。下面是同样运作良好,对我两种选择。
选项1 - 最优雅的;只能运行在有问题,而不是所有项目的项目的FindControl。
保护小组NewProduct_ItemDataBound(BYVAL发件人为ListView控件,BYVAL E上ListViewItemEventArgs)处理NewProduct.ItemDataBound
昏暗我为整数= sender.EditIndex
如果我= e.Item.DataItemIndex然后
朦胧DDL作为DropDownList的= DirectCast(e.Item.FindControl(NewProductName_ddl),DropDownList的)
昏暗TB作为文本框= DirectCast(e.Item.FindControl(NewProductName_cb),文本框) ddl.Items.FindByValue(sender.DataKeys(I)(ID)。的ToString).Selected = TRUE'prefills将DropDownList
tb.Text = sender.DataKeys(I)(产品)。的ToString'prefills文本框
万一
结束小组
选项2 - 基于引用的问题,但有一个关键的检查,以确保非空对象
保护小组NewProduct_ItemDataBound(BYVAL发件人为ListView控件,BYVAL E上ListViewItemEventArgs)处理NewProduct.ItemDataBound
昏暗我为整数= sender.EditIndex
如果我&GT;然后-1
朦胧DDL作为DropDownList的= DirectCast(e.Item.FindControl(NewProductName_ddl),DropDownList的)
昏暗TB作为文本框= DirectCast(e.Item.FindControl(NewProductName_cb),文本框) 如果不IsNothing(DDL)然后
ddl.Items.FindByValue(sender.DataKeys(I)(ID)。的ToString).Selected = TRUE'prefills将DropDownList
万一
如果未IsNothing(TB)。然后
tb.Text = sender.DataKeys(I)(产品)。的ToString'prefills文本框
万一
万一
结束小组
的我可能会作出改善,这个答案后,但这样做有我。 :)的
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
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. :)
这篇关于关于访问的ListView编辑命令控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!