2012-01-24 49 views
0

在我的ListView我有一个ItemTemplateEditItemTemplate看起来像这样,分别。ListView编辑命令访问控制

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

当我点击“编辑”按钮,切换到右侧的EditItemTemplate的看法,我想预先填入Textbox并选择相应的optionDropDownList。我怎样才能做到这一点?

在您说要使用类似以下内容之前,请知道我已经探索了我能想到的所有可能的变体。对不起,要求如此高,但如果你回答,请准备好引导我完成这一个。^^我一直停留在这个问题上的字面几个月:(

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 
+1

有你看这里http://stackoverflow.com/questions/825048/accessing-controls-in-the-edititemtemplate-of-a -列表显示。您尝试访问该控件的哪个事件? –

+0

@PaulMcCowat是的,现在我正在更仔细地观察它,但我仍然无法连接点。如何“告诉”ItemDataBound DropDownList SelectedValue应该来自ItemCommand事件?即如何在事件之间传递数据?或者我在想它错了?看看我有多困惑? :'( – Chiramisu

+0

看起来您正在查找'NewProductName_ddl',就好像它是一个实例一样,这可能会返回一个DropDownLists集合。如果没有完整的HTML和代码可用,很难帮助您编写代码。 –

回答

2

EUREKA!

我很高兴超乎想象!所有大写字母,也不是大胆的正义,我现在有多幸福:)

首先我想给道具this question,这让我指出了正确的方向。现在回答,这是我发现上述链接中提供的答案中最理想的变体:

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 

我可以作出改善,这个答案后,但是这并获得成功对我来说。 :)

2

好帖子!我遇到了同样的问题,你救了我几个小时的试验和错误。只是想指出,在.NET Framework 3.5或更低版本中使用第一个选项时,DataItemIndex不可用。要解决它,你可以代替

If i = e.Item.DataItemIndex Then 

随着

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

很高兴知道。谢谢。;) – Chiramisu

+0

@NoA:真棒,谢谢你的 –