Nested ListView control
i m having the following hierarchy in my aspx page
<asp:ListView ID="ListView1" DataSourceID="SqlDataSource1" runat="server">
<ItemTemplate>
...
&l开发者_如何学Pythont;asp:ListView ID="ListView2" DataKeyNames="statusID" runat="server"
DataSourceID="SqlDataSource2"
ItemPlaceholderID="pl"
OnItemCommand="ListView2_ItemCommand">
<LayoutTemplate>
<asp:PlaceHolder ID="pl" runat="server"/>
...
<asp:Button ID="Button2" runat="server" Text="Post"/>
</LayoutTemplate>
<ItemTemplate>
...
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
now if i click Button2 then ListView2_ItemCommand event is fired.
protected void ListView2_ItemCommand(object sender, ListViewCommandEventArgs e)
inside the handler e.item is null, why?
there should be ItemTemplate
with CommandArgument
E.g.
<ItemTemplate>
<asp:ImageButton ID="ImgBtn" runat="server" CommandName="Select" CommandArgument='<%#Eval("ProductId")%>' />
</ItemTemplate>
- ListView.ItemCommand [MSDN]
How do you bind that controls? Check does ListView2 is not bind again after PostBack. If this is bind again Command will be canceled.
Why are you using nested list view? Did you consider using Groups http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.groups.aspx?
In general, if you have a ListView lvInner nested inside a ListView lvOuter, use the handler lvOuter_ItemCommand and the CommandSource property of the ListViewCommandEventArgs.
The following worked for me:
protected void lvOuter_ItemCommand(object sender, ListViewCommandEventArgs e)
{
ListView lvInner = (ListView)e.Item.FindControl("lvInner");
lvInner.SelectedIndex = lvInner.Items.IndexOf((ListViewDataItem)e.CommandSource);
int innerID = (int)lvInner.SelectedDataKey["InnerID"];
// etc.
}
Please note that in this case lvInner is nested inside the ItemTemplate of lvOuter.
精彩评论