首页 技术 正文
技术 2022年11月7日
0 收藏 722 点赞 745 浏览 6354 个字

前台代码:

 <%@ 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="external nofollow" 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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781