或许大家会说,网上已经很多类似文章了,包括孟子的,为什么要再写一次?
我想我们不仅仅要会实现,更多的是需要理解。
下面先帖出代码,再分析一下其中的一些关键代码。
数据库表名:tb1,其中有3个字段,分别是ID自增的主键、vName varchar(50)、iAge int
(以下代码没有做任何错误捕获处理)
前台
<%
@ Page language
=
"
c#
"
Codebehind
=
"
WebForm5.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
csdn.WebForm5
"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
title
>
WebForm5
</
title
>
<
meta
content
="Microsoft Visual Studio .NET 7.1"
name
="GENERATOR"
>
<
meta
content
="C#"
name
="CODE_LANGUAGE"
>
<
meta
content
="JavaScript"
name
="vs_defaultClientScript"
>
<
meta
content
="http://schemas.microsoft.com/intellisense/ie5"
name
="vs_targetSchema"
>
</
HEAD
>
<
body
>
<
form
id
="Form1"
method
="post"
runat
="server"
>
<
asp:textbox
id
="TextBox1"
runat
="server"
Width
="88px"
></
asp:textbox
>
<
asp:TextBox
id
="TextBox2"
runat
="server"
Width
="40px"
></
asp:TextBox
>
<
asp:Button
id
="Button1"
runat
="server"
Text
="添加"
></
asp:Button
>
<
asp:datagrid
id
="DataGrid1"
runat
="server"
AutoGenerateColumns
="False"
DataKeyField
="ID"
AllowPaging
="True"
PageSize
="5"
OnEditCommand
="edit"
OnCancelCommand
="cancel"
OnUpdateCommand
="update"
>
<
Columns
>
<
asp:TemplateColumn
HeaderText
="姓名"
>
<
ItemTemplate
>
<%
# DataBinder.
Eval
(Container.DataItem,
"
vName
"
)
%>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
id
="name"
Runat
="server"
Text
='<%#
DataBinder.Eval(Container.DataItem,"vName") %
>
' Width="88px">
</
asp:TextBox
>
</
EditItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="年龄"
>
<
ItemTemplate
>
<%
# DataBinder.
Eval
(Container.DataItem,
"
iAge
"
)
%>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
id
="age"
Runat
="server"
Text
='<%#
DataBinder.Eval(Container.DataItem,"iAge") %
>
' Width="40px">
</
asp:TextBox
>
</
EditItemTemplate
>
</
asp:TemplateColumn
>
<
asp:EditCommandColumn
UpdateText
="更新"
CancelText
="取消"
EditText
="编辑"
></
asp:EditCommandColumn
>
<
asp:ButtonColumn
Text
="删除"
CommandName
="del"
></
asp:ButtonColumn
>
</
Columns
>
<
PagerStyle
Mode
="NumericPages"
></
PagerStyle
>
</
asp:datagrid
></
form
>
</
body
>
</
HTML
>
后台
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
csdn
{
///
<summary>
///
WebForm5 的摘要说明。
///
</summary>
public
class
WebForm5 : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.TextBox TextBox1;
protected
System.Web.UI.WebControls.TextBox TextBox2;
protected
System.Web.UI.WebControls.Button Button1;
protected
System.Web.UI.WebControls.DataGrid DataGrid1;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
在此处放置用户代码以初始化页面
if
(
!
IsPostBack)
{
SetBind();
}
}
protected
void
SetBind()
{
SqlConnection conn
=
new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[
"
conn
"
]);
SqlDataAdapter da
=
new
SqlDataAdapter(
"
select * from tb1
"
,conn);
DataSet ds
=
new
DataSet();
da.Fill(ds,
"
table1
"
);
this
.DataGrid1.DataSource
=
ds.Tables[
"
table1
"
];
this
.DataGrid1.DataBind();
}
Web 窗体设计器生成的代码
private
void
Button1_Click(
object
sender, System.EventArgs e)
{
SqlConnection conn
=
new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[
"
conn
"
]);
SqlCommand comm
=
new
SqlCommand(
"
insert into tb1 (vName,iAge) values (@vName,@iAge)
"
,conn);
SqlParameter parm1
=
new
SqlParameter(
"
@vName
"
,SqlDbType.NVarChar,
50
);
parm1.Value
=
this
.TextBox1.Text;
SqlParameter parm2
=
new
SqlParameter(
"
@iAge
"
,SqlDbType.Int);
parm2.Value
=
this
.TextBox2.Text;
comm.Parameters.Add(parm1);
comm.Parameters.Add(parm2);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
SetBind();
}
private
void
DataGrid1_ItemCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if
(e.CommandName
==
"
del
"
)
{
SqlConnection conn
=
new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[
"
conn
"
]);
SqlCommand comm
=
new
SqlCommand(
"
delete from tb1 where ID=@id
"
,conn);
SqlParameter parm1
=
new
SqlParameter(
"
@id
"
,SqlDbType.Int);
parm1.Value
=
this
.DataGrid1.DataKeys[e.Item.ItemIndex];
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
SetBind();
}
}
private
void
DataGrid1_PageIndexChanged(
object
source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this
.DataGrid1.CurrentPageIndex
=
e.NewPageIndex;
SetBind();
}
protected
void
edit(
object
sender,DataGridCommandEventArgs e)
{
this
.DataGrid1.EditItemIndex
=
e.Item.ItemIndex;
SetBind();
}
protected
void
cancel(
object
sender,DataGridCommandEventArgs e)
{
w