** (ASP.NET) ** ** 修改和删除 ** ** DataGrid ** ** 行——数据库访问 ** ** **
本程序涉及到数据库的添加,修改和删除操作。
懒得写了,把界面贴出来,照着界面画就可以了。本例数据库: SqlServer2000 附带的 pubs 数据库,看一下连接字符串就很清楚了。如果要在本机器上运行,把 uid 和 pwd 改成你自己 SQL 登陆用户名和密码。
创建一个 WEB 页面,命名为: Add.aspx 。
界面设计如图:

Add.aspx 代码:
1@ Page language="c#" Codebehind="Add.aspx.cs" AutoEventWireup="false" Inherits="TeachShow.Charpter7.AccessDataBase.Add"
1<html>
2<head>
3<title>Add</title>
4<link href="../../Style.css" rel="stylesheet" type="text/css"/>
5<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
6<meta content="C#" name="CODE_LANGUAGE"/>
7<meta content="JavaScript" name="vs_defaultClientScript"/>
8<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"/>
9</head>
10<body ms_positioning="GridLayout">
11<form id="Form1" method="post" runat="server">
12<div align="center">
13<center>
14<table border="0" cellpadding="0" cellspacing="0" class="smallBlack" height="318" width="429">
15<tr>
16<td class="title" colspan="2" height="31" valign="top" width="429"> 添加一个新的发行者 </td>
17</tr>
18<tr>
19<td height="23" valign="top" width="79"> 发行者 ID : </td>
20<td height="23" valign="top" width="350"><asp:textbox cssclass="smallRed" height="18px" id="TextBox1" runat="server"></asp:textbox><font face=" 宋体 ">( 以 99 打头,共 4 位数字 )</font></td>
21</tr>
22<tr>
23<td height="23" valign="top" width="79"><font face=" 宋体 "> 姓名: </font></td>
24<td height="23" valign="top" width="350"><asp:textbox cssclass="smallRed" height="18px" id="TextBox2" runat="server"></asp:textbox></td>
25</tr>
26<tr>
27<td height="23" valign="top" width="79"><font face=" 宋体 "> 城市: </font></td>
28<td height="23" valign="top" width="350"><asp:textbox cssclass="smallRed" height="18px" id="TextBox3" runat="server"></asp:textbox></td>
29</tr>
30<tr>
31<td height="23" valign="top" width="79"><font face=" 宋体 "> 省份: </font></td>
32<td height="23" valign="top" width="350"><asp:textbox cssclass="smallRed" height="18px" id="TextBox4" runat="server"></asp:textbox><font face=" 宋体 ">(2 个字符 )</font></td>
33</tr>
34<tr>
35<td height="24" valign="top" width="79"><font face=" 宋体 "> 国家: </font></td>
36<td height="24" valign="top" width="350"><asp:textbox cssclass="smallRed" height="18px" id="TextBox5" runat="server"></asp:textbox></td>
37</tr>
38<tr>
39<td align="center" colspan="2" height="24" valign="top" width="429"><asp:linkbutton id="LinkButton1" runat="server"> 提交到数据库 </asp:linkbutton></td>
40</tr>
41<tr>
42<td colspan="2" height="147" valign="top" width="429">
43<asp:datagrid cssclass="general" height="120px" id="DataGrid1" runat="server" width="428px">
44<itemstyle width="50px"></itemstyle>
45<columns>
46<asp:editcommandcolumn buttontype="LinkButton" canceltext=" 取消 " edittext=" 编辑 " updatetext=" 更新 ">
47<headerstyle width="60px"></headerstyle>
48</asp:editcommandcolumn>
49<asp:buttoncolumn commandname="Delete" text=" 删除 "></asp:buttoncolumn>
50</columns>
51</asp:datagrid></td>
52</tr>
53</table>
54</center>
55</div>
56</form>
57</body>
58</html>
Add.asp.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 TeachShow.Charpter7.AccessDataBase
{
///
1<summary>
2
3/// Add 的摘要说明。
4
5/// </summary>
public class Add : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox5;
private void Page_Load( object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (! this .IsPostBack)
{
this .BindGrid();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base .OnInit(e);
}
///
1<summary>
2
3/// 设计器支持所需的方法 - 不要使用代码编辑器修改
4
5/// 此方法的内容。
6
7/// </summary>
private void InitializeComponent()
{
this .LinkButton1.Click += new System.EventHandler( this .LinkButton1_Click);
this .DataGrid1.Disposed += new System.EventHandler( this .DataGrid1_Disposed);
this .DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler( this .DataGrid1_CancelCommand);
this .DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler( this .DataGrid1_EditCommand);
this .DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler( this .DataGrid1_UpdateCommand);
this .DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler( this .DataGrid1_DeleteCommand);
this .DataGrid1.SelectedIndexChanged += new System.EventHandler( this .DataGrid1_SelectedIndexChanged);
this .Load += new System.EventHandler( this .Page_Load);
}
#endregion
private void LinkButton1_Click( object sender, System.EventArgs e)
{
AddPublisher();
}
<span lang="EN" style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体;