在我的ListView
我有一个ItemTemplate
和EditItemTemplate
看起来像这样,分别。ListView编辑命令访问控制
------->
当我点击“编辑”按钮,切换到右侧的EditItemTemplate
的看法,我想预先填入Textbox
并选择相应的option
DropDownList
。我怎样才能做到这一点?
在您说要使用类似以下内容之前,请知道我已经探索了我能想到的所有可能的变体。对不起,要求如此高,但如果你回答,请准备好引导我完成这一个。^^我一直停留在这个问题上的字面几个月:(
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!
噢,我的善良再用,如此接近,但没有雪茄下面的代码工作为预填的时候只有一个项目是在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
有你看这里http://stackoverflow.com/questions/825048/accessing-controls-in-the-edititemtemplate-of-a -列表显示。您尝试访问该控件的哪个事件? –
@PaulMcCowat是的,现在我正在更仔细地观察它,但我仍然无法连接点。如何“告诉”ItemDataBound DropDownList SelectedValue应该来自ItemCommand事件?即如何在事件之间传递数据?或者我在想它错了?看看我有多困惑? :'( – Chiramisu
看起来您正在查找'NewProductName_ddl',就好像它是一个实例一样,这可能会返回一个DropDownLists集合。如果没有完整的HTML和代码可用,很难帮助您编写代码。 –