我使用的CommandField删除图像按钮,但它不工作。下面是我尝试过的一个片段:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
int indx = row.RowIndex;
using (Property_dbDataContext context = new Property_dbDataContext())
{
if (e.CommandName == "Delete")
{
var delete = (from a in context.Property_basics where a.prop_id == GridView1.Rows[indx].Cells[0].Text select a).Single();
delete.delete_status = 1;
context.SubmitChanges();
GridView1.DeleteRow(indx);
}
}
}.aspx:
<asp:CommandField ButtonType="Image" HeaderText="Delete" DeleteImageUrl="~/wp-content/themes/realia/assets/img/500px-Delete_Icon2.png" ShowDeleteButton="True" />它所显示的例外是:
无法将类型为'System.Web.UI.WebControls.ContentPlaceHolder‘的对象转换为“System.Web.UI.WebControls.GridViewRow”。
发布于 2014-11-17 08:28:44
试试这个:
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer.Parent.Parent;或者就像
设置命令参数
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = ProductsGridView.Rows[index];发布于 2014-11-17 08:32:06
您应该使用CommandArgument来获取RowIndex,而不是搜索行。在CommandField的情况下,CommandArgument就是RowIndex。对于传递给标准控件的CommandArgument,它是实际的命令参数。
int rowIndex = int.Parse(e.CommandArgument);
GridViewRow row = GridView1.Rows[rowIndex];发布于 2014-11-17 12:34:36
您的ASPX将如下所示:
<asp:Gridview id = Gridview1 runat = "server" DataKeyNames="ID" > 代码背后:你可以这样做,
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(dg1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)dg1.Rows[e.RowIndex];
// write your code. to delete
}https://stackoverflow.com/questions/26968391
复制相似问题