设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP.NET

一个使用GridView显示数据,并且可以进行添加、修改、删除操作的例子

作者:佚名 出处:网络转载 时间:08-24 点击:

内容载入中...
教程中国版权声明: 华腾联合.中国

<%@ 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里面的全部记录.

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

收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息