日期:2014-05-18  浏览次数:20432 次

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>
/////////////////////////////////////////////////