我正在尝试对GridView
. 根据我在网上阅读的内容,有几种方法可以执行此类操作。所有这些方法看起来都非常复杂,并且涉及到大量的手动管道。
例如,为了从 a 中“删除”一个项目GridView
,我遇到了以下方法:
1:使用GridView
RowCommand
事件:
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gridviewRow =
CoordinateFilesGridView.Rows[Convert.ToInt32(e.CommandArgument)];
MyEntity entity = (MyEntity)gridviewRow.DataItem;
if(e.CommandName.Equals("Delete"))
{
// Perform delete action
Delete(entity);
}
}
OnClick
2:向“删除”按钮提供事件
public void Delete_Clicked(Object sender, System.EventArgs e)
{
var item = ((sender as WebControl).NamingContainer as DataListItem);
var rowID = int.Parse(((HiddenField)item.FindControl("rowID")).Value);
GridViewRow gridviewRow = CoordinateFilesGridView.Rows[rowID];
MyEntity entity = (MyEntity)gridviewRow.DataItem;
// Perform delete action
Delete(entity);
}
3:链接到一个 URL 并在Page_Load
if (queryString != null && queryString["action"] != null)
{
if (queryString["action"].Equals("delete") && queryString["rowID"] != null)
{
GridViewRow gridviewRow =
CoordinateFilesGridView.Rows[(int)queryString["rowID"]];
MyEntity entity = (MyEntity)gridviewRow.DataItem;
// Perform delete action
Delete(entity);
}
}
您将如何执行这样的操作?有一个更好的方法吗?