<asp:ListView ID="lvProducts" runat="server" DataSourceID="SqlDataSource1"
OnItemCommand="lvProducts_ItemCommand" ItemPlaceholderID="PlaceHolder1">
<LayoutTemplate>
<asp:Placeholder
id="PlaceHolder1"
runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("ProductID") %>'
Text='<%# Eval("ProductName") %>' CommandName="View Details"
runat="server"></asp:LinkButton><br />
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]">
</asp:SqlDataSource>
C#
protected void lvProducts_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "View Details")
{
int productId = (int)e.CommandArgument;
}
}
VB.NET
Protected Sub lvProducts_ItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
If e.CommandName = "View Details" Then
Dim productId As Integer = CInt(e.CommandArgument)
End If
End Sub
Tweet
1 comment:
Many thanks - this is really clear and allows me to get out of a hole I had dug for myself!
Post a Comment