c#
private void DataGrid1_ItemBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
e.Item.Cells[删除按钮的索引].Attributes.Add("onClick","return confirm('你确定要删除这一项吗?');");
}
private void DataGrid1_ItemBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
e.Item.Cells[删除按钮的索引].Attributes.Add("onClick","return confirm('你确定要删除这一项吗?');");
}
Public Sub DataGrid2_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid2.ItemCreated
If (e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item) Then
CType(e.Item.FindControl("LinkButton7"), LinkButton).Attributes.Add("onclick", "return confirm('您真的要删除第" & (e.Item.ItemIndex + 1).ToString() & " 行吗?')")
End If
End Sub
{
btnDelete.Attributes.Add("onclick", "if(confirm('是否真的要删除?')){document.getElementById('Hidden1').value='你要返回的值'}else{return false;}");
}
With dgTask (datagrid控件名)
.DataSource = dsTask
.DataBind()
If dsTask.Tables(0).Rows.Count > 0 Then
For nLoop = 0 To .Items.Count - 1
Dim dBtn As LinkButton
Dim lUser As Label
dBtn = CType(.Items(nLoop).FindControl("btnDelete"), LinkButton)
lUser = CType(.Items(nLoop).FindControl("lblUser"), Label)
dBtn.Attributes.Add("OnClick", "javascript:return window.confirm('确认要删除吗?');")
dsUser = p_DBMan.getUserFullname(Session("SysConn"), Session("strAreaID"), lUser.Text.Replace(" ", ""))
If dsUser.Tables(0).Rows.Count > 0 Then
lUser.Text = dsUser.Tables(0).Rows(0)("strUserFullName")
End If If fIsProvinceUser(Session("strAreaId")) = True Then '如果为省级用户隐藏删改,提交等
CType(.Items(nLoop).FindControl("btnDelete"), LinkButton).Visible = False
CType(.Items(nLoop).FindControl("btnEdit"), LinkButton).Visible = False
CType(.Items(nLoop).FindControl("btnSubmit"), LinkButton).Visible = False End If
Next
Else
Response.Write(fShowMsg(modPublic.ueMsgType.ActionError, "没有记录"))
End If
End With
在DATAGRID的模块中这样写上
<asp:TemplateColumn HeaderText="操作">
<HeaderStyle CssClass="Grid_HeaderStyle"></HeaderStyle>
<ItemStyle Width="120px" CssClass="dgboder"></ItemStyle>
<ItemTemplate>
<asp:LinkButton id="btnSelect" runat="server" Text="查看" CausesValidation="false" CommandArgument="select" CommandName="Edit"></asp:LinkButton>
<asp:LinkButton id="btnEdit" runat="server" Text="修改" CausesValidation="false" CommandArgument="edit" CommandName="Edit"></asp:LinkButton>
<asp:LinkButton id="btnSubmit" runat="server" Text="提交" CausesValidation="false" CommandArgument="submit" CommandName="Edit"></asp:LinkButton>
<asp:LinkButton id="btnDelete" runat="server" Text="删除" CausesValidation="false" CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
试试看,我的就是这样做的。
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %><script language="VB" runat="server">
dim MyConnection as OleDbConnection
dim cmdSelect as OleDbCommand
dim dtrReader as OleDbDataReader
Sub Page_load(sender as object,e as Eventargs)
MyConnection = New OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\datagrid.mdb")
if not IsPostBack() then Mydatagrid_DataBind() 'list all
end if
End SubSub Mydatagrid_DataBind()
Dim MyCommand As OleDbDataAdapter = new OleDbDataAdapter("select * from news order by id desc", MyConnection) Dim DS As DataSet = new DataSet()
MyCommand.Fill(DS, "news")
MyDataGrid.DataSource=DS.Tables("news").DefaultView
MyDataGrid.DataBind()
End Subsub Data_Del(obj as object, e as DataGridCommandEventArgs)
Dim MyCommand As OleDbCommand
Dim DeleteCmd As String = "DELETE from news where id = @Id" MyCommand = New OleDbCommand(DeleteCmd, MyConnection)
MyCommand.Parameters.Add(New OleDbParameter("@Id", OleDbType.VarChar, 11))
MyCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
MyCommand.Connection.Open()
MyCommand.ExecuteNonQuery()
MyCommand.Connection.Close() Mydatagrid_DataBind()
End sub Sub DataGrid_ItemCreated(Sender As Object, e As DataGridItemEventArgs) Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem
Dim myTableCell As TableCell
myTableCell = e.Item.Cells(0)
Dim myDeleteButton As LinkButton
myDeleteButton = myTableCell.Controls(0)
myDeleteButton.Attributes.Add("onclick", _
"return confirm('Are you Sure you want to delete this recoders?');")
End Select
End Sub
</script>
<html>
<head>
<title>DropDownList.aspx</title>
<link rel="stylesheet" href="css.css" type="text/css">
</head>
<body>
<form Runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="100"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="true"
AllowPaging="false"
pageSize="2"
DataKeyField="id"
OnDeleteCommand="Data_Del"
OnItemCreated="DataGrid_ItemCreated"
>
<Columns>
<asp:ButtonColumn HeaderText="" text="Del" CommandName="Delete"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>