C# 如何从code behinde访问按钮并在ListView ItemTemplate中单击时执行一些工作?

C# 如何从code behinde访问按钮并在ListView ItemTemplate中单击时执行一些工作?,c#,asp.net,listview,button,click,C#,Asp.net,Listview,Button,Click,.aspx 正确的方法是将CommandName参数添加到按钮中,然后在EventHandler中捕获该参数。示例如下: protected void ListViewUsers_ItemCommand(object sender, ListViewCommandEventArgs e) { // How to access the button, and do some work when it is clicked. } 参考MSDN文章 protected void List

.aspx


正确的方法是将CommandName参数添加到按钮中,然后在EventHandler中捕获该参数。示例如下:

protected void ListViewUsers_ItemCommand(object sender, ListViewCommandEventArgs e)
{
      // How to access the button, and do some work when it is clicked.
}
参考MSDN文章

protected void ListViewUsers_ItemCommand(object sender, ListViewCommandEventArgs e)
{
      // How to access the button, and do some work when it is clicked.
}
<asp:Button ID="Btn_addFriend" runat="server"  Text="Add friend" CommandName="Add friend" />
protected void ListViewUsers_ItemCommand(object sender,ListViewCommandEventArgs e)
{
  if(String.Equals(e.CommandName, "Add friend"))
  {
    // ... DO SOME WORK HERE
  }
}