/*UpdateDelForm.aspx
前台程序代码
*/
<%@ Page language="c#" Codebehind="UpdateDelForm.aspx.cs"
AutoEventWireup="false" Inherits="UpdateDelDbDemo.UpdateDelForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="vs_snapToGrid" content="False">
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="dgDemo" runat="server" AutoGenerateColumns="False" GridLines="Horizontal">
<Columns>
<asp:BoundColumn DataField="ID" ReadOnly="True" HeaderText="UserID"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="FirstName"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="LastName"></asp:BoundColumn>
<asp:BoundColumn DataField="address" HeaderText="Address"></asp:BoundColumn>
<asp:EditCommandColumn
ButtonType="LinkButton" UpdateText="更新" CancelText="取消"
EditText="编辑"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid></FONT>
</form>
</body>
</HTML>
/*UpdateDelForm.aspx.cs
后台程序代码
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace UpdateDelDbDemo
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class UpdateDelForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgDemo;
private string strCon="server=JOSEN;database=testDb;integrated security=true";
private string strSqlSelect="select * from UserInfo";
private SqlConnection objCon;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
LoadGrid();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgDemo.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_CancelCommand);
this.dgDemo.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_EditCommand);
this.dgDemo.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_UpdateCommand);
this.dgDemo.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//读取数据并帮定到DataGrid
private void LoadGrid()
{
Connect();
SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);
DataSet ds=new DataSet();
objAdpt.Fill(ds,"dtUserInfo");
dgDemo.DataSource=ds;
dgDemo.DataBind();
}
//建立数据库的连接
private void Connect()
{
if(objCon==null)
objCon=new SqlConnection(strCon);
if(objCon.State==ConnectionState.Closed)
objCon.Open();
}
//断开数据库的连接
private void Disconnect()
{
objCon.Close();
}
private void dgDemo_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDemo.EditItemIndex=e.Item.ItemIndex;
LoadGrid();
}
private void dgDemo_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDemo.EditItemIndex=-1;
LoadGrid();
}
private void dgDemo_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strUserID=e.Item.Cells[0].Text;
//string strUserID=((TextBox)e.Item.Cells[0].Controls[0]).Text;
string strFirstName=((TextBox)e.Item.Cells[1].Controls[0]).Text;
string strLastName=((TextBox)e.Item.Cells[2].Controls[0]).Text;
string strAddress=((TextBox)e.Item.Cells[3].Controls[0]).Text;
dgDemo.EditItemIndex=-1;
//此方法具体处理数据库的更新
UpdateRecord(strUserID,strFirstName,strLastName,strAddress);
//调用此方法从数据库读取数据
LoadGrid();
}
private void UpdateRecord(string strUserID, string strFirstName, string strLastName, string strAddress)
{
Connect();
SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);
DataSet ds=new DataSet();
objAdpt.Fill(ds,"dtUserInfo");
Disconnect();
DataTable tbl=ds.Tables["dtUserInfo"];
//设定表的主键
DataColumn[] myKey=new DataColumn[1];
myKey[0]=tbl.Columns["ID"];
tbl.PrimaryKey=myKey;
DataRow dr=tbl.Rows.Find(strUserID);
//dr["ID"]=strUserID;
dr["FirstName"]=strFirstName;
dr["LastName"]=strLastName;
dr["address"]=strAddress;
SqlCommandBuilder cb=new SqlCommandBuilder(objAdpt);
Connect();
objAdpt.Update(ds,"dtUserInfo");
Disconnect();
}
private void dgDemo_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strUserID=e.Item.Cells[0].Text;
dgDemo.EditItemIndex=-1;
DelRecord(strUserID);
//调用此方法从数据库读取数据
LoadGrid();
}
private void DelRecord(string strUserID)
{
Connect();
SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);
DataSet ds=new DataSet();
objAdpt.Fill(ds,"dtUserInfo");
Disconnect();
DataTable tbl=ds.Tables["dtUserInfo"];
//设定表的主键
DataColumn[] myKey=new DataColumn[1];
myKey[0]=tbl.Columns["ID"];
tbl.PrimaryKey=myKey;
DataRow dr=tbl.Rows.Find(strUserID);
dr.Delete();
SqlCommandBuilder cb=new SqlCommandBuilder(objAdpt);
Connect();
objAdpt.Update(ds,"dtUserInfo");
Disconnect();
}
}
}
文章来源:
http://www.blogjava.net/kuxiaoku/articles/94807.html
posted on 2007-01-19 00:15
苦笑枯 阅读(348)
评论(0) 编辑 收藏 所属分类:
C#