代码之家  ›  专栏  ›  技术社区  ›  Abe Miessler

如何在ListView中捕获事件?

  •  1
  • Abe Miessler  · 技术社区  · 14 年前

    我可以用下面的代码来做,但看起来很粗糙。有没有更好的办法?

    ((ListViewDataItem)((Button)sender).Parent.Parent)
    

    更新:

    ((ListViewDataItem)((Button)sender).NamingContainer)
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Brian Mains    14 年前

    是,给按钮一个命令名,然后附加到ListView.ItemCommand;单击按钮触发这个事件,它有一些关于列表项的更多细节,比如通过e.item引用它。

        2
  •  0
  •   kelsier Cameron    10 年前
    protected void RemoveButton_Click(object sender, EventArgs e)
            {
                ListViewDataItem item = ((ListViewDataItem)((Button)sender).NamingContainer);
                //ListViewDataItem item = (ListViewDataItem)((LinkButton)sender).Parent;
    
                int i = item.DisplayIndex;
                DataTable dt = (DataTable)Session["cart"];
                dt.Rows[i].Delete();
    
                Listcart.DataSource = dt;
                Listcart.DataBind();
    
                Label Lblcart = (Label)Page.Master.FindControl("Lbitem");
                Lblcart.Text = Listcart.Controls.Count.ToString();
                Session["quantity"] = Lblcart.Text;
                Session["cart"] = dt;
                GrandTotal();
                Session["amount"] = LbGrandTotal.Text;
    
            }