ASP.NET探寻

itzhiren的Blog

导航

<%@ Page Language="VB" Debug ="true"  %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("rizhi.mdb") & ";User

Id=;Password=;"
    Dim sql As String
    Dim mycommand As OleDbCommand
    Dim myread As OleDbDataReader
    Dim conn As OleDbConnection
    Sub page_load(ByVal sender As Object, ByVal e As EventArgs)
        conn = New OleDbConnection(connstr)
        conn.Open()
        sql = "select * from rizhi"
        mycommand = New OleDbCommand(sql, conn)
        myread = mycommand.ExecuteReader()
       
        GridView1.DataSource = myread
        GridView1.DataBind()
        conn.Close()
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub
    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        Dim id As Integer = GridView1.DataKeys(e.RowIndex).Value
        Dim j As Integer = 0
        conn = New OleDbConnection(connstr)
        conn.Open()
        sql = "delete from rizhi where id=" & id
       
        mycommand = New OleDbCommand(sql, conn)
        Try
            j = mycommand.ExecuteNonQuery
            Response.Write("共删除了" & j & "条记录!")
            sql = "select * from rizhi"
            mycommand = New OleDbCommand(sql, conn)
            myread = mycommand.ExecuteReader()
       
            GridView1.DataSource = myread
            GridView1.DataBind()

        Catch ex As Exception
            Throw ex
        Finally
            conn.Close()
        End Try
       
       
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <center>
    <a href ="rizhi_add.aspx" style ="font-size :12px;">增加新日志</a>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width ="60%" Font-Size="12px"

OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="id" >
        <Columns>
        <asp:BoundField DataField="id" HeaderText="序号" >
            <ItemStyle Font-Size="12px" HorizontalAlign="Left" />
            <HeaderStyle BackColor="BurlyWood" Font-Size="12px" HorizontalAlign="Center" />
        </asp:BoundField>
        <asp:BoundField DataField="rq" HeaderText="日期" >
    <ItemStyle Font-Size="12px" HorizontalAlign="Left" />
    <HeaderStyle BackColor="BurlyWood" Font-Size="12px" HorizontalAlign="Center" />
</asp:BoundField>

        <asp:BoundField DataField="n_je" HeaderText="金额" >
            <ItemStyle Font-Size="12px" HorizontalAlign="Right" />
            <HeaderStyle BackColor="BurlyWood" Font-Size="12px" HorizontalAlign="Center" />
        </asp:BoundField>
       
        <asp:BoundField DataField="title" HeaderText="标题" >
            <ItemStyle Font-Size="12px" HorizontalAlign="Left" />
            <HeaderStyle BackColor="BurlyWood" Font-Size="12px" HorizontalAlign="Center" />
        </asp:BoundField>
       
        <asp:BoundField DataField="content" HeaderText="内容" >
            <ItemStyle Font-Size="12px" HorizontalAlign="Left" />
            <HeaderStyle BackColor="BurlyWood" Font-Size="12px" HorizontalAlign="Center" />
        </asp:BoundField>
       
        <asp:HyperLinkField DataTextField="id" DataNavigateUrlFields ="id"

DataNavigateUrlFormatString="rizhi_edit.aspx?id={0}" HeaderText ="操作" DataTextFormatString ="修改" >
        <ItemStyle Font-Size="12px" HorizontalAlign="Center" />
        <HeaderStyle BackColor ="burlyWood" Font-Size="12px" HorizontalAlign="center" />
        </asp:HyperLinkField>
        <asp:CommandField DeleteText ="删除" HeaderText="操作" ButtonType="Link"  ShowDeleteButton="true" />
        </Columns>
        </asp:GridView>
    </center>      
    </div>
    </form>
</body>
</html>


显示rizhi表的所有记录,并且可以对这些记录进行修改和删除,并且可以添加新日志
添加新日志时跳转到rizhi_add.aspx页面进行添加;
修改是跳转到rizhi_edit.aspx页面进行,传递一个参数id;
删除操作,直接在本页面进行,在
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width ="60%" Font-Size="12px"

OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="id" >
中可以看到,删除操作响应的是GridView1_RowDeleting事件,在GridView1_RowDeleting中可以看到,删除了要删除的记录之后,需要对GridView1

进行重新绑定,显示删除记录之后数据库表rizhi里面的全部记录.

该页面进行的删除,是直接删除该记录,而没有进行提示"是否确认删除该记录",有关让用户确认之后再删除记录的方法,会在以后的文章中提到.