gridview删除问题 在线等,急
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add( "onclick ", "javascript:return confirm( '你确认要删除:\ " " + e.Row.Cells[1].Text + "\ "吗? ') ");
}
}
}
"e.Row.Cells[1].Text "是主键 我从哪写LinkButton删除事件
[解决办法]
设置LinkButton的CommandName= "Delete "
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//根据主键来删 GridView1.DataKeys[e.RowIndex][0]);
}
[解决办法]
<asp:TemplateColumn HeaderText= "操作 ">
<HeaderStyle Width= "20% "> </HeaderStyle>
<ItemTemplate>
<asp:LinkButton id= "LinkButton2 " runat= "server " CommandName= "delete "> 删除 </asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName== "delete ")
{
string Sql= "delete T_Province where Province_ID= "+e.Item.Cells[0].Text+ " ";
....
//执行操作
}
}
[解决办法]
有点帖乱了,贴VS2003了,改成写到VS2005类似的事件中
[解决办法]
设置datakeysname啊 然后GridView1.DataKeys[e.RowIndex][0]);
[解决办法]
int.Parse(e.CommandArgument.ToString()) 这个可以获得
[解决办法]
.aspx页面
<asp:TemplateField HeaderText= "删除 " ShowHeader= "False " >
<ItemTemplate>
<asp:ImageButton ID= "ImageButton1 " runat= "server " CausesValidation= "False " OnClientClick= "return confirm( '确定删除当前记录? '); " CommandName= "Delete "
ImageUrl= "~/Imgs/Delete.gif " Text= "删除 "
/>
</ItemTemplate>
</asp:TemplateField>
/////////////////////////////////////////////////
.cs 页面
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow gvr = this.GridView1.Rows[e.RowIndex];//e.RowIndex为指定行的索引
}
[解决办法]
加上string ID= gvr.Cells[0].Text;
再根据id删除
[解决办法]
gridview命令,也可传值出去,到另外页面去处理删除事件