【发布时间】:2012-02-18 03:09:43
【问题描述】:
在我的 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
【问题讨论】:
-
你看过这里stackoverflow.com/questions/825048/…。您尝试在哪个事件中访问控件?
-
@PaulMcCowat 是的,我现在正在更仔细地查看它,但我仍然无法连接这些点。如何“告诉” ItemDataBound DropDownList SelectedValue 应该来自 ItemCommand 事件?即我如何在事件之间传递该数据?还是我想错了?看看我有多迷茫? :'(
-
看起来您正在寻找
NewProductName_ddl,就好像它是一个实例一样。这可能会返回 DropDownLists 的集合。在没有完整的 html 和代码可用的情况下,很难帮助您编写代码。
标签: asp.net vb.net listview initialization findcontrol