views:

35

answers:

1

Hi,
i am new in ListView Control. I have a List view to show shopping products. in each data Item i put a link-button for "Add to cart" button. in my scenario clicking on this button causes ShoppingCart.Instance.AddItem("Product GUID") to call. how can i perform that?
i set CommandName="Select" in Link Button and performed this:

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...
}

but always the selectedIndex is Zero!!! How can I call ShoppingCart.Instance.AddItem("Product GUID") and get Product Id from DataItem???
note: Eval("ID") gets Product GUID.

Update:
I Set <%# Eval("ID")%> for CommandArgument of LinkButton and i want somthing linke this:

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

    var productId = new Guid(lb.CommandArgument);

    ShoppingCart.Instance.AddItem(productId);
}
A: 

you can acces control inside selected row with itemcommand event of ListView Control:

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;
}
AEMLoviji
you need to format your code example given above so that the comment and code are separate.
Mamta Dalal
your code just returns CommandArgument of first Data Item. I put '<%# Eval("ID")%>' for CommandArgument of LinkButton but always the st is first Item ID. I updated my question please notice.
mahdiahmadirad