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

关于DataGrid删除记录的问题(网上答案很多,但都不够详细,还是没有帮我解决问题,请高手帮忙)
我写的代码如下:
ASPX代码:
<asp:ButtonColumn Text="&lt;img border=0 alt='删除' src=../index/images/delete.gif&gt;"
  HeaderText="删除"
  ButtonType="LinkButton" 
  HeaderStyle-ForeColor="#FFFFFF"
  CommandName="Delete_Click" /> 


ASPX.VB代码:
Protected Sub Delete_Click(ByVal Sender As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
  Dim StrID = e.Item.Cells(0).Text
  Dim MyConn As OleDbConnection, MyCmd As OleDbCommand, MySql As String
  MyConn = New OleDbConnection(ConfigurationManager.AppSettings("AccessString") & System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings("DataBase")))
  MyConn.Open()

  MySql = "Delete * from Equipment Where C_ID=" & StrID
  MyCmd = New OleDbCommand(MySql, MyConn)
  MyCmd.ExecuteNonQuery()
  MyConn.Close()
End Sub
请各位高手帮忙

------解决方案--------------------
<!--在DataGrid控件里添加一个"OnDeleteCommand">
<asp:DataGrid ID="MyGrid" runat="server"
......
OnDeleteCommand="Delete_Click"
AutoGenerateColumns="false"
.....>
<Columns>
<!--如果是自定义字段,那就增加一个id,传给VB代码-->
<asp:BoundColumn DataField="id" Visible="false">
<!--在增加一个删除链接(按钮)-->
<asp:ButtonColumn Text= "<img src='../index/images/delete.gif' OnClick='JavaScript:return delAction()' border='0'>" CommandName="Delete" HeaderText="操作" HeaderStyle-ForeColor="#FFFFFF" />
</Columns>
</asp:DataGrid>

另外::JavaScript:return delAction()是一个JavaScript的验证,提示用户是否确定删除,其他的应该看得懂了吧?