如何在DataBound之后获取ListView中的DataItem的Id

时间:2010-09-07 06:28:26

标签: c# asp.net listview

我是ListView Control的新手。我有一个列表视图来显示购物产品。在每个数据项目中,我为“添加到购物车”按钮添加了一个链接按钮。在我的方案中,单击此按钮会导致ShoppingCart.Instance.AddItem("Product GUID")调用。我该怎么做呢? 我在链接按钮中设置了CommandName="Select"并执行了此操作:

protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
    ListViewItem item = (ListViewItem)ListView1.Items[e.NewSelectedIndex];
    LinkButton lb = (LinkButton)item.FindControl("LinkButtonAddAndClose");
// Here i want to get selected Product Id...
}

但是selectedIndex总是零! 如何调用ShoppingCart.Instance.AddItem("Product GUID")并从DataItem获取产品ID? 注意:Eval("ID")获取产品GUID。

更新
我为LinkBut​​ton的CommandArgument设置了<%# Eval("ID")%>,我想要这样做:

protected void LinkButtonAddAndClose_Click(object sender, EventArgs e)
{
    LinkButton lb = (LinkButton)sender;

    var productId = new Guid(lb.CommandArgument);

    ShoppingCart.Instance.AddItem(productId);
}

1 个答案:

答案 0 :(得分:0)

您可以使用ListView控件的itemcommand事件访问所选行内的控件:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
   //for example i want to take commandargumetn atribute of current linkbutton
   string st= (e.Item.FindControl("LinkButtonAddAndClose") as LinkButton).CommandArgument;
}