GridView 和 Access数据库实现数据绑定(asp.net)
前台代码:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="主页.aspx.cs" Inherits="主页" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <link href="Styles/main.css" rel="stylesheet" type="text/css" />
- </head>
- <body bgcolor="#ffffcc">
- <form id="form1" runat="server">
- <div style="font-family: 幼圆; font-size: larger; color: #FF00FF; height: 29px; width: 1340px;">
- <asp:GridView ID="gvw" DataKeyNames="id" runat="server" AllowPaging="True" AllowSorting="True"
- AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
- AutoGenerateSelectButton="True" DataSourceID="AccessDataSrc" Style="font-size: large"
- Width="732px" OnRowUpdating="gvw_RowUpdating" OnRowDataBound="gvw_RowDataBound">
- <Columns>
- <asp:BoundField DataField="id" ReadOnly="true" InsertVisible="true" HeaderText="id" />
- <asp:CommandField CancelText="取消" EditText="编辑" UpdateText="更新" DeleteText="删除" />
- <asp:TemplateField HeaderText="编号" runat="Server" SortExpression="num">
- <EditItemTemplate>
- <asp:TextBox Text='<%# Bind("num") %>' ID="txtNum" runat="server" />
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblNum" runat="server" Text='<%# Bind("num") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="姓名">
- <EditItemTemplate>
- <asp:TextBox Text='<%# Bind("name") %>' ID="txtName" runat="server" />
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblName" runat="server" Text='<%# Bind("name") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="性别">
- <EditItemTemplate>
- <asp:TextBox Text='<%# Bind("sex") %>' ID="txtSex" runat="server" />
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblSex" runat="server" Text='<%# Bind("sex") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="联系方式" SortExpression="contact">
- <EditItemTemplate>
- <asp:TextBox Text='<%# Bind("contact") %>' ID="txtContact" runat="server" />
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblContact" runat="server" Text='<%# Bind("contact") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="所在地">
- <EditItemTemplate>
- <asp:TextBox Text='<%# Bind("address") %>' ID="txtAddress" runat="server" />
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblAddress" runat="server" Text='<%# Bind("address") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- <asp:LinkButton ID="lbtnAddNew" runat="Server" OnClick="lkbtnAddNew_Click">AddNew</asp:LinkButton>
- <asp:Panel runat="server" ID="pnlAdd" Visible="false">
- <table id="addNew">
- <tr>
- <td>
- <label for="txtNum">
- 编号:</label>
- </td>
- <td>
- <asp:TextBox ID="txtNum" runat="Server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- <label for="txtName">
- 姓名:</label>
- </td>
- <td>
- <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- <label for="txtSex">
- 性别:</label>
- </td>
- <td>
- <asp:TextBox ID="txtSex" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- <label for="txtContact">
- 联系方式:</label>
- </td>
- <td>
- <asp:TextBox ID="txtContact" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- <label for="txtAddress">
- 住址:</label>
- </td>
- <td>
- <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
- </td>
- </tr>
- </table>
- <asp:LinkButton ID="lbtnConfirm" runat="Server" OnClick="btnConfirm_Click">Submit</asp:LinkButton>
- <asp:LinkButton ID="lbtnCancel" runat="server" OnClick="btnCancel_Click">Cancel</asp:LinkButton>
- </asp:Panel>
- <asp:AccessDataSource ID="AccessDataSrc" runat="server" DataFile="~/website/App_Data/people2003.mdb"
- SelectCommand="SELECT * FROM [t_01]" DeleteCommand="delete from [t_01] where id=@id"
- InsertCommand="insert into [t_01]([num],[name],[sex],[contact],[address]) values(@num,@name,@sex,@contact,@address)">
- <DeleteParameters>
- <asp:Parameter Name="id" />
- </DeleteParameters>
- <InsertParameters>
- <asp:ControlParameter ControlID="txtNum" Name="num" Type="String" PropertyName="Text" />
- <asp:ControlParameter ControlID="txtName" Name="name" Type="String" PropertyName="Text" />
- <asp:ControlParameter ControlID="txtSex" Name="sex" Type="String" PropertyName="Text" />
- <asp:ControlParameter ControlID="txtContact" Name="contact" Type="String" PropertyName="Text" />
- <asp:ControlParameter ControlID="txtAddress" Name="address" Type="String" PropertyName="Text" />
- </InsertParameters>
- </asp:AccessDataSource>
- </div>
- </form>
- </body>
- </html>
后台代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class 主页 : System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
- }
- //update.
- protected void gvw_RowUpdating(object sender, GridViewUpdateEventArgs e) {
- AccessDataSrc.UpdateCommand = "update [t_01] set [num]=@num,name=@name,sex=@sex,contact=@contact,address=@address where [id]=@id";
- string num = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtNum")).Text.Trim();
- string name = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtName")).Text.Trim();
- string sex = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtSex")).Text.Trim();
- string contact = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtContact")).Text.Trim();
- string address = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtAddress")).Text.Trim();
- AccessDataSrc.UpdateParameters.Add("num", TypeCode.String, num);
- AccessDataSrc.UpdateParameters.Add("name", TypeCode.String, name);
- AccessDataSrc.UpdateParameters.Add("sex", TypeCode.String, sex);
- AccessDataSrc.UpdateParameters.Add("contact", TypeCode.String, contact);
- AccessDataSrc.UpdateParameters.Add("address", TypeCode.String, address);
- AccessDataSrc.DataBind();
- }
- //Confirm to perform delete operation.
- protected void gvw_RowDataBound(object sender, GridViewRowEventArgs e) {
- if (e.Row.RowType == DataControlRowType.DataRow) {
- if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) {
- LinkButton btnDelete = (LinkButton)e.Row.Cells[].Controls[];
- btnDelete.Attributes["onclick"] = "if(!confirm('Centain to delete?')) return false";
- //var c = gvw.Rows[e.Row.RowIndex].Cells[0].Controls[0];
- }
- }
- }
- //Confirm to add new.
- protected void btnConfirm_Click(object sender, EventArgs e) {
- try {
- AccessDataSrc.Insert();
- txtNum.Text = "";
- txtName.Text = "";
- txtSex.Text = "";
- txtContact.Text = "";
- txtAddress.Text = "";
- Response.Write("<script>alert('添加成功!')</script>");
- }
- catch (System.Exception ex) {
- }
- }
- //Cancel to add new.
- protected void btnCancel_Click(object sender, EventArgs e) {
- txtNum.Text = "";
- txtName.Text = "";
- txtSex.Text = "";
- txtContact.Text = "";
- txtAddress.Text = "";
- pnlAdd.Visible = false;
- lbtnAddNew.Visible = true;
- }
- //Add new.
- protected void lkbtnAddNew_Click(object sender, EventArgs e) {
- pnlAdd.Visible = true;
- lbtnAddNew.Visible = false;
- }
- }
GridView 和 Access数据库实现数据绑定(asp.net)的更多相关文章
- ASP连接读写ACCESS数据库实例(转)
(一) 数据库的选择:有许多的数据库你可以选择,SQL SERVER.ACCESS(*.mdb).EXCEL(*.xls).FOXPRO(*.dbf)甚至普通的文本文件(*.txt)都可以达到存储 ...
- ACCESS数据库注入
0X01 我们想来了解一下access数据库 Access注入是暴力猜解 Access数据结构(access只有一个数据库) Access数据库 表名 列名 数据 没有库这个概念 只有表这个概念 这应 ...
- 如何在Asp.net中备份Access数据库?
public void Create( string mdbPath ) { if( File.Exists(mdbPath) ) //检查数据库是否已存在 { thr ...
- asp.net连接SQL server,SQLLite,Oracle,Access数据库
asp.net中连接数据库有两种方式为appSettings和connectionStrings connectionStrings比较常用,所以只讲一下connectionStrings方式的连接 ...
- ASP入门(二)-创建Access数据库
通常来说,ASP程序是搭配Access数据库来使用的,因此在安装完ASP环境后,为了方便建立和管理数据库,我们还需要安装Access数据库. Access是Microsoft Office家族中的一员 ...
- 用asp连接Access数据库 制作简单登陆界面
[题外话:最近做Internet作业,在这写一个适合初学入门的ASP连接ACCESS数据库做登陆界面的简单的例子,以慰藉我一口气把以前做过的系统中的PHP代码全改成ASP代码来临时应付作业的心情... ...
- asp.net mvc access数据库操作
连接access数据库其实也简单,只要按照mvc的模式来就可以,遵循c v约定就可以 既然渲染试图是强类型,那么取得的数据就转换成强类型,其他一切和asp.net操作一样 DB mydb = new ...
- ASP.NET中防止Access数据库下载
如何防止Access数据库下载是一个很老的话题了,网上的讨论也比较多.这里我们给出几种在ASP.NET下防止Access数据库被下载的方法. 我们这里假设Access数据库名字为 test.mdb. ...
- asp.net连接Access数据库实现登陆功能
这里话就不多说了,直接演示代码. 连接access数据库首先需要配置web.config <appSettings> <add key="AccessConnString& ...
随机推荐
- linux vi 使用脑图
- 用异提交时,后台通过校验规则文件,校验不通过时,跳转到INPUT视图时,前台显示错误信息的解决办法
1.第一种: 最近项目使用了struts2的校验(其实我觉得后台校验,特别是struts的校验,完全可以放在其他地方处理,比如交给js或者业务逻辑),而且系统刚好还使用了extjs,此时问题出现了:假 ...
- 汇编下的i++与++i
故事背景,一个正在c语言的家伙,问我++i 和 i++的问题,我当时因为要去上课没给他说,正好今晚有空就测试了一下如下代码: 编译环境:VS2010 语言:C++ #include <iost ...
- 转:浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案
综述 随着越来越多的组织的数据从GB.TB级迈向PB级,标志着整个社会的信息化水平正在迈入新的时代 – 大数据时代.对海量数据的处理.分析能力,日益成为组织在这个时代决胜未来的关键因素,而基于大数据的 ...
- Linux下配置QT环境
一.下载Qt源码包到本机,然后解压缩 #tar zxvf qt-x11-opensource-src-4.3.2.tar.gz -C /usr/local //将qt-x11-opensource-s ...
- android merge 标签的使用
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <ToggleButton ...
- 移动web app开发框架
文章地址:http://www.cnblogs.com/soulaz/p/5586787.html jQuery Mobile jQuery Mobile框架能够帮助你快速开发出支持多种移动设备的Mo ...
- cf202-div 1-B - Apple Tree:搜索,数论,树的遍历
http://codeforces.com/contest/348/problem/B B. Apple Tree time limit per test 2 seconds memory l ...
- java与数据结构(8)---java实现链队列
链队列 实际上就是单链表,只是规定了删除在队头进行,添加在队尾进行. 链队列代码结构 package list.queue; public interface Queuable<T>; p ...
- Q - Get The Treasury - HDU 3642 (扫面线求体积)
题意:求被三个或三个以上立方体重合的体积 分析:就是平面面积的加强,不过归根还是一样的,可以把z轴按照从小向大分区间N个,然后可以得到N个平面,用平面重复三次以上的在和高度计算体积. ******** ...