前台代码:

  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 id=@id"
  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]=@num,name=@name,sex=@sex,contact=@contact,address=@address where [id]=@id";
  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[].Controls[];
  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. }

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

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

    (一)   数据库的选择:有许多的数据库你可以选择,SQL SERVER.ACCESS(*.mdb).EXCEL(*.xls).FOXPRO(*.dbf)甚至普通的文本文件(*.txt)都可以达到存储 ...

  2. ACCESS数据库注入

    0X01 我们想来了解一下access数据库 Access注入是暴力猜解 Access数据结构(access只有一个数据库) Access数据库 表名 列名 数据 没有库这个概念 只有表这个概念 这应 ...

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

    public   void   Create(   string   mdbPath   ) { if(   File.Exists(mdbPath)   )   //检查数据库是否已存在 { thr ...

  4. asp.net连接SQL server,SQLLite,Oracle,Access数据库

    asp.net中连接数据库有两种方式为appSettings和connectionStrings connectionStrings比较常用,所以只讲一下connectionStrings方式的连接 ...

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

    通常来说,ASP程序是搭配Access数据库来使用的,因此在安装完ASP环境后,为了方便建立和管理数据库,我们还需要安装Access数据库. Access是Microsoft Office家族中的一员 ...

  6. 用asp连接Access数据库 制作简单登陆界面

    [题外话:最近做Internet作业,在这写一个适合初学入门的ASP连接ACCESS数据库做登陆界面的简单的例子,以慰藉我一口气把以前做过的系统中的PHP代码全改成ASP代码来临时应付作业的心情... ...

  7. asp.net mvc access数据库操作

    连接access数据库其实也简单,只要按照mvc的模式来就可以,遵循c v约定就可以 既然渲染试图是强类型,那么取得的数据就转换成强类型,其他一切和asp.net操作一样 DB mydb = new ...

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

    如何防止Access数据库下载是一个很老的话题了,网上的讨论也比较多.这里我们给出几种在ASP.NET下防止Access数据库被下载的方法. 我们这里假设Access数据库名字为 test.mdb. ...

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

    这里话就不多说了,直接演示代码. 连接access数据库首先需要配置web.config <appSettings> <add key="AccessConnString& ...

随机推荐

  1. linux vi 使用脑图

  2. 用异提交时,后台通过校验规则文件,校验不通过时,跳转到INPUT视图时,前台显示错误信息的解决办法

    1.第一种: 最近项目使用了struts2的校验(其实我觉得后台校验,特别是struts的校验,完全可以放在其他地方处理,比如交给js或者业务逻辑),而且系统刚好还使用了extjs,此时问题出现了:假 ...

  3. 汇编下的i++与++i

    故事背景,一个正在c语言的家伙,问我++i 和 i++的问题,我当时因为要去上课没给他说,正好今晚有空就测试了一下如下代码: 编译环境:VS2010  语言:C++ #include <iost ...

  4. 转:浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案

    综述 随着越来越多的组织的数据从GB.TB级迈向PB级,标志着整个社会的信息化水平正在迈入新的时代 – 大数据时代.对海量数据的处理.分析能力,日益成为组织在这个时代决胜未来的关键因素,而基于大数据的 ...

  5. Linux下配置QT环境

    一.下载Qt源码包到本机,然后解压缩 #tar zxvf qt-x11-opensource-src-4.3.2.tar.gz -C /usr/local //将qt-x11-opensource-s ...

  6. android merge 标签的使用

    <merge xmlns:android="http://schemas.android.com/apk/res/android"> <ToggleButton ...

  7. 移动web app开发框架

    文章地址:http://www.cnblogs.com/soulaz/p/5586787.html jQuery Mobile jQuery Mobile框架能够帮助你快速开发出支持多种移动设备的Mo ...

  8. cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

      http://codeforces.com/contest/348/problem/B   B. Apple Tree time limit per test 2 seconds memory l ...

  9. java与数据结构(8)---java实现链队列

    链队列 实际上就是单链表,只是规定了删除在队头进行,添加在队尾进行. 链队列代码结构 package list.queue; public interface Queuable<T>; p ...

  10. Q - Get The Treasury - HDU 3642 (扫面线求体积)

    题意:求被三个或三个以上立方体重合的体积 分析:就是平面面积的加强,不过归根还是一样的,可以把z轴按照从小向大分区间N个,然后可以得到N个平面,用平面重复三次以上的在和高度计算体积. ******** ...