GridView 和 Access数据库实现数据绑定(asp.net)

前台代码:

  1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="主页.aspx.cs" Inherits="主页" %>
  2
  3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4 <html xmlns="http://www.w3.org/1999/xhtml">
  5 <head runat="server">
  6     <title></title>
  7     <link href="Styles/main.css" rel="stylesheet" type="text/css" />
  8 </head>
  9 <body bgcolor="#ffffcc">
 10     <form id="form1" runat="server">
 11     <div style="font-family: 幼圆; font-size: larger; color: #FF00FF; height: 29px; width: 1340px;">
 12         <asp:GridView ID="gvw" DataKeyNames="id" runat="server" AllowPaging="True" AllowSorting="True"
 13             AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
 14             AutoGenerateSelectButton="True" DataSourceID="AccessDataSrc" Style="font-size: large"
 15             Width="732px" OnRowUpdating="gvw_RowUpdating" OnRowDataBound="gvw_RowDataBound">
 16             <Columns>
 17                 <asp:BoundField DataField="id" ReadOnly="true" InsertVisible="true" HeaderText="id" />
 18                 <asp:CommandField CancelText="取消" EditText="编辑" UpdateText="更新" DeleteText="删除" />
 19                 <asp:TemplateField HeaderText="编号" runat="Server" SortExpression="num">
 20                     <EditItemTemplate>
 21                         <asp:TextBox Text=‘<%# Bind("num") %>‘ ID="txtNum" runat="server" />
 22                     </EditItemTemplate>
 23                     <ItemTemplate>
 24                         <asp:Label ID="lblNum" runat="server" Text=‘<%# Bind("num") %>‘></asp:Label>
 25                     </ItemTemplate>
 26                 </asp:TemplateField>
 27                 <asp:TemplateField HeaderText="姓名">
 28                     <EditItemTemplate>
 29                         <asp:TextBox Text=‘<%# Bind("name") %>‘ ID="txtName" runat="server" />
 30                     </EditItemTemplate>
 31                     <ItemTemplate>
 32                         <asp:Label ID="lblName" runat="server" Text=‘<%# Bind("name") %>‘></asp:Label>
 33                     </ItemTemplate>
 34                 </asp:TemplateField>
 35                 <asp:TemplateField HeaderText="性别">
 36                     <EditItemTemplate>
 37                         <asp:TextBox Text=‘<%# Bind("sex") %>‘ ID="txtSex" runat="server" />
 38                     </EditItemTemplate>
 39                     <ItemTemplate>
 40                         <asp:Label ID="lblSex" runat="server" Text=‘<%# Bind("sex") %>‘></asp:Label>
 41                     </ItemTemplate>
 42                 </asp:TemplateField>
 43                 <asp:TemplateField HeaderText="联系方式" SortExpression="contact">
 44                     <EditItemTemplate>
 45                         <asp:TextBox Text=‘<%# Bind("contact") %>‘ ID="txtContact" runat="server" />
 46                     </EditItemTemplate>
 47                     <ItemTemplate>
 48                         <asp:Label ID="lblContact" runat="server" Text=‘<%# Bind("contact") %>‘></asp:Label>
 49                     </ItemTemplate>
 50                 </asp:TemplateField>
 51                 <asp:TemplateField HeaderText="所在地">
 52                     <EditItemTemplate>
 53                         <asp:TextBox Text=‘<%# Bind("address") %>‘ ID="txtAddress" runat="server" />
 54                     </EditItemTemplate>
 55                     <ItemTemplate>
 56                         <asp:Label ID="lblAddress" runat="server" Text=‘<%# Bind("address") %>‘></asp:Label>
 57                     </ItemTemplate>
 58                 </asp:TemplateField>
 59             </Columns>
 60         </asp:GridView>
 61         <asp:LinkButton ID="lbtnAddNew" runat="Server" OnClick="lkbtnAddNew_Click">AddNew</asp:LinkButton>
 62         <asp:Panel runat="server" ID="pnlAdd" Visible="false">
 63             <table id="addNew">
 64                 <tr>
 65                     <td>
 66                         <label for="txtNum">
 67                             编号:</label>
 68                     </td>
 69                     <td>
 70                         <asp:TextBox ID="txtNum" runat="Server"></asp:TextBox>
 71                     </td>
 72                 </tr>
 73                 <tr>
 74                     <td>
 75                         <label for="txtName">
 76                             姓名:</label>
 77                     </td>
 78                     <td>
 79                         <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
 80                     </td>
 81                 </tr>
 82                 <tr>
 83                     <td>
 84                         <label for="txtSex">
 85                             性别:</label>
 86                     </td>
 87                     <td>
 88                         <asp:TextBox ID="txtSex" runat="server"></asp:TextBox>
 89                     </td>
 90                 </tr>
 91                 <tr>
 92                     <td>
 93                         <label for="txtContact">
 94                             联系方式:</label>
 95                     </td>
 96                     <td>
 97                         <asp:TextBox ID="txtContact" runat="server"></asp:TextBox>
 98                     </td>
 99                 </tr>
100                 <tr>
101                     <td>
102                         <label for="txtAddress">
103                             住址:</label>
104                     </td>
105                     <td>
106                         <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
107                     </td>
108                 </tr>
109             </table>
110             <asp:LinkButton ID="lbtnConfirm" runat="Server" OnClick="btnConfirm_Click">Submit</asp:LinkButton>
111             <asp:LinkButton ID="lbtnCancel" runat="server" OnClick="btnCancel_Click">Cancel</asp:LinkButton>
112         </asp:Panel>
113         <asp:AccessDataSource ID="AccessDataSrc" runat="server" DataFile="~/website/App_Data/people2003.mdb"
114             SelectCommand="SELECT * FROM [t_01]" DeleteCommand="delete from [t_01] where [email protected]"
115             InsertCommand="insert into [t_01]([num],[name],[sex],[contact],[address]) values(@num,@name,@sex,@contact,@address)">
116             <DeleteParameters>
117                 <asp:Parameter Name="id" />
118             </DeleteParameters>
119             <InsertParameters>
120                 <asp:ControlParameter ControlID="txtNum" Name="num" Type="String" PropertyName="Text" />
121                 <asp:ControlParameter ControlID="txtName" Name="name" Type="String" PropertyName="Text" />
122                 <asp:ControlParameter ControlID="txtSex" Name="sex" Type="String" PropertyName="Text" />
123                 <asp:ControlParameter ControlID="txtContact" Name="contact" Type="String" PropertyName="Text" />
124                 <asp:ControlParameter ControlID="txtAddress" Name="address" Type="String" PropertyName="Text" />
125             </InsertParameters>
126         </asp:AccessDataSource>
127     </div>
128     </form>
129 </body>
130 </html>

后台代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7
 8 public partial class 主页 : System.Web.UI.Page {
 9     protected void Page_Load(object sender, EventArgs e) {
10     }
11     //update.
12     protected void gvw_RowUpdating(object sender, GridViewUpdateEventArgs e) {
13         AccessDataSrc.UpdateCommand = "update [t_01] set [num][email protected],[email protected],[email protected],[email protected],[email protected] where [id][email protected]";
14
15         string num = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtNum")).Text.Trim();
16         string name = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtName")).Text.Trim();
17         string sex = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtSex")).Text.Trim();
18         string contact = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtContact")).Text.Trim();
19         string address = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtAddress")).Text.Trim();
20
21         AccessDataSrc.UpdateParameters.Add("num", TypeCode.String, num);
22         AccessDataSrc.UpdateParameters.Add("name", TypeCode.String, name);
23         AccessDataSrc.UpdateParameters.Add("sex", TypeCode.String, sex);
24         AccessDataSrc.UpdateParameters.Add("contact", TypeCode.String, contact);
25         AccessDataSrc.UpdateParameters.Add("address", TypeCode.String, address);
26         AccessDataSrc.DataBind();
27     }
28     //Confirm to perform delete operation.
29     protected void gvw_RowDataBound(object sender, GridViewRowEventArgs e) {
30         if (e.Row.RowType == DataControlRowType.DataRow) {
31             if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) {
32                 LinkButton btnDelete = (LinkButton)e.Row.Cells[0].Controls[2];
33                 btnDelete.Attributes["onclick"] = "if(!confirm(‘Centain to delete?‘)) return false";
34                 //var c = gvw.Rows[e.Row.RowIndex].Cells[0].Controls[0];
35             }
36         }
37     }
38     //Confirm to add new.
39     protected void btnConfirm_Click(object sender, EventArgs e) {
40         try {
41             AccessDataSrc.Insert();
42             txtNum.Text = "";
43             txtName.Text = "";
44             txtSex.Text = "";
45             txtContact.Text = "";
46             txtAddress.Text = "";
47             Response.Write("<script>alert(‘添加成功!‘)</script>");
48         }
49         catch (System.Exception ex) {
50
51         }
52     }
53     //Cancel to add new.
54     protected void btnCancel_Click(object sender, EventArgs e) {
55         txtNum.Text = "";
56         txtName.Text = "";
57         txtSex.Text = "";
58         txtContact.Text = "";
59         txtAddress.Text = "";
60         pnlAdd.Visible = false;
61         lbtnAddNew.Visible = true;
62     }
63     //Add new.
64     protected void lkbtnAddNew_Click(object sender, EventArgs e) {
65         pnlAdd.Visible = true;
66         lbtnAddNew.Visible = false;
67     }
68 }
时间: 2024-08-05 08:23:39

GridView 和 Access数据库实现数据绑定(asp.net)的相关文章

ASP连接读写ACCESS数据库实例(转)

(一)   数据库的选择:有许多的数据库你可以选择,SQL SERVER.ACCESS(*.mdb).EXCEL(*.xls).FOXPRO(*.dbf)甚至普通的文本文件(*.txt)都可以达到存储数据的功能.那么到底选择什么数据库最合适呢?其中SQLSERVER是服务器级的,足以应付每天上万次的访问量.对于非服务器级的数据库建议使用ACCESS97. 因为:(1)只要安装了PWS或IIS4肯定有ACCESS的ODBC驱动,即你的系统肯定支持ACCESS数据库的存取.(2)ACCESS支持的S

如何在Asp.net中备份Access数据库?

public   void   Create(   string   mdbPath   ) { if(   File.Exists(mdbPath)   )   //检查数据库是否已存在 { throw   new   Exception( "目标数据库已存在,无法创建 "); } //   可以加上密码,这样创建后的数据库必须输入密码后才能打开 mdbPath   =   "Provider=Microsoft.Jet.OLEDB.4.0;Data   Source= &

ASP.NET中防止Access数据库下载

如何防止Access数据库下载是一个很老的话题了,网上的讨论也比较多.这里我们给出几种在ASP.NET下防止Access数据库被下载的方法. 我们这里假设Access数据库名字为 test.mdb. 1.把数据库放在WEB目录外 如你的网站目录是D:\www,你可以把数据库放到D:\data 这个文件夹里,然后修改网站程序中的数据库连接字串地址部分为:"D:\data\test.mdb" ,这样数据库可以正常调用,但是无法下载的,因为它不在网站目录里. 假设在web.config中配置

【ASP】ASP对Access数据库的连接、增删改查及ASP的基本语法

本文讨论的是ASP,而不是Visual Studio写出来的.ASPX也就是ASP.NET. ASP不需要任何插件的支持,关键你是配置好WINDOWS的IIS服务器,把页面扔在上面执行就可以了. 不同WINDOWS系统的IIS服务器配置,除了某些被阉割的WINXP装机版系统之外,都支持IIS服务器配置,都在WINDOWS的组件中,不需要下载任何东西.如何配置IIS服务器不是本文讨论的重点. 一.基本目标 假设Access2007数据库database.mdb中存在表test如下: id为自增列,

asp.net连接Access数据库实现登陆功能

这里话就不多说了,直接演示代码. 连接access数据库首先需要配置web.config <appSettings> <add key="AccessConnString" value="provider=microsoft.jet.oledb.4.0;data source="/> <add key="AccessDbPath" value="~/App_Data/News.mdb"/>

ASP登陆界面连接ACCESS数据库

1,(index.asp 用户登陆页面)<!-- #include file="conn.asp" --><!-- blog.soowooo.cn 悠悠长假期 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>会员</title><

asp.net 连接access数据库方法

在 Web.Config 中配置 Access 数据库驱动和数据库文件名称. 请看代码 <appSettings> <add key="DBDriver" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source ="/> <add key="DBName" value="Company.mdb"/> </appSettings>

ASP入门(二)-创建Access数据库

通常来说,ASP程序是搭配Access数据库来使用的,因此在安装完ASP环境后,为了方便建立和管理数据库,我们还需要安装Access数据库. Access是Microsoft Office家族中的一员,主要提供了一个轻量级的数据库解决方案. 本人安装的是Office 2013版本的Access(SW_DVD5_Office_Professional_Plus_2013_64Bit_ChnSimp_MLF_X18-55285.ISO),普及率最高的还是Office 2003,如果你使用XP或者Wi

初学asp.net 中access数据库和各种数据库的通用连接(原创)

之前从事asp的开发,转用 asp.net 开发,记录下相关操作流程备忘. 文章本人原创,本人是新手,高手请无视,谢谢! 缘由:之前用asp写网站,都会用到 access 数据库,网上搜索asp.net 连接access数据库,得到的方法多数是用 ado.net 或者是手动配置 web.config ,走了不少弯路,后来发现asp.net其实把事情已经帮我们做好了,直接用就可以了,本文以 Visual Studio 2010 为例,列出所有操作步骤. 一.新建网站: 文件 --  新建 -- 网