<%@ Page Language="vb" Debug="true" %>
 <%@ Import Namespace =Namespace="System.Data" %>
 <%@ Import Namespace =Namespace="System.Data.OleDb" %>

<script language="VB" runat="server">
dim MyConnection as OleDbConnection
dim cmdSelect as OleDbCommand
dim dtrReader as OleDbDataReader
 Sub Page_load()Sub Page_load(sender as object,e as Eventargs)
MyConnection = New OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath(".")+"/db/datagrid.mdb" )
if not IsPostBack() then

Mydatagrid_DataBind()
end if
End Sub

 Sub Mydatagrid_DataBind()Sub Mydatagrid_DataBind()
Dim MyCommand As OleDbDataAdapter = new OleDbDataAdapter("select * from news order by id asc", MyConnection)

Dim DS As DataSet = new DataSet()
MyCommand.Fill(DS, "news")
MyDataGrid.DataSource=DS.Tables("news").DefaultView
MyDataGrid.DataBind()
End Sub

 Sub Data_Del()sub 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 dg_ItemDataBound()Sub dg_ItemDataBound(sender as object , e as DataGridItemEventArgs )
If (e.Item.ItemType = ListItemType.Item or e.Item.ItemType = ListItemType.AlternatingItem) then
Dim myTableCell As TableCell
myTableCell = e.Item.Cells(0)
Dim myDeleteButton As LinkButton
myDeleteButton = myTableCell.Controls(0)
myDeleteButton.Attributes.Add("onclick", "return confirm('Delete ID = " + e.Item.Cells(1).Text + " Record" + "?');")
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#F4EE71'")
if e.Item.ItemType = ListItemType.Item then
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'")
end if
if e.Item.ItemType = ListItemType.AlternatingItem then
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#FCF9CB'")
end if
End If
End Sub
</script>

<form Runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="100"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#F0D26A"
AutoGenerateColumns="true"
AllowPaging="false"
pageSize="2"
DataKeyField="id"
OnDeleteCommand="Data_Del"
OnItemDataBound="dg_ItemDataBound" >
<ItemStyle BackColor="#ffffff"/>
<AlternatingItemStyle BackColor="#FCF9CB"/>

<Columns>
<asp:ButtonColumn HeaderText="" text="Del" CommandName="Delete"/>
</Columns>
</asp:DataGrid>
</form>
发表于
2004-11-13 11:19
菲一打
阅读( 873)
评论()
收藏
举报
|