我使用了以下两种不同的方式来实现对数据的编辑和删除
第一种方式:把主要的代码写在aspx页面内[实现了编辑和删除].cs文件内不需要添加任何代码
注:
1、 <add name="myTest" connectionString="Data source=.;Database=myTest;Integrated Security=True" providerName="System.Data.SqlClient" />
2、DataKeyNames="userid"这句如果少掉话会提示你缺少userid参数
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridTest.aspx.cs" Inherits="gridTest" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="userSource" runat="server" ConnectionString="<%$ ConnectionStrings:myTest %>"
DeleteCommand="Delete From users where userid=@userid"
SelectCommand="Select userid,username,sex from users"
InsertCommand="insert into users values(@username,@sex,@)"
UpdateCommand="Update users set username=@username,sex=@sex where userid=@userid">
<DeleteParameters>
<asp:Parameter Name="userid" Type="int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="userid" Type="int32" />
<asp:Parameter Name="username" Type="String" />
<asp:Parameter Name="sex" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="gdTest" DataSourceID="userSource" DataKeyNames="userid" runat="server" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" PageSize="3">
<Columns>
<asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />
<asp:BoundField HeaderText="ID" DataField="userid" InsertVisible="false" ReadOnly="true" SortExpression="userid" />
<asp:BoundField HeaderText="Name" DataField="username" SortExpression="username" />
<asp:BoundField HeaderText="Sex" DataField="sex" SortExpression="sex" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
******************************************
第二种方式:主要代码在.cs文件内,实现了添加删除编辑
.cs文件
--------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class gridSourceProcedure : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_init(object sender, EventArgs e)
{
getData();
}
public void getData()
{
try
{
sdsUsers.ConnectionString = ConfigurationManager.ConnectionStrings["myTest"].ToString();
sdsUsers.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
sdsUsers.DeleteCommand = "delete_users";
sdsUsers.DeleteParameters.Add(new Parameter("userid",TypeCode.Int32));
sdsUsers.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
sdsUsers.InsertCommand = "insert_users";
//值取自窗体中的这些控件。。参数名,控件ID
sdsUsers.InsertParameters.Add(new FormParameter("username", "txtName"));
sdsUsers.InsertParameters.Add(new FormParameter("sex", "txtSex"));
sdsUsers.InsertParameters.Add(new FormParameter("age", "txtAge"));
sdsUsers.InsertParameters.Add(new FormParameter("email", "txtEmail"));
sdsUsers.InsertParameters.Add(new FormParameter("address", "txtAddress"));
sdsUsers.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
sdsUsers.SelectCommand = "select_users";
sdsUsers.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
sdsUsers.UpdateCommand = "update_users";
sdsUsers.UpdateParameters.Add(new Parameter("userid", TypeCode.Int32));
sdsUsers.UpdateParameters.Add(new Parameter("sex", TypeCode.String));
sdsUsers.UpdateParameters.Add(new Parameter("age", TypeCode.Int32));
sdsUsers.UpdateParameters.Add(new Parameter("email", TypeCode.String));
sdsUsers.UpdateParameters.Add(new Parameter("address", TypeCode.String));
//GridView setting
gridViewTest.AllowPaging = true;
gridViewTest.AllowSorting = true;
gridViewTest.AutoGenerateColumns = false;
gridViewTest.DataSourceID = "sdsUsers";
gridViewTest.PageSize = 5;
CommandField cmd = new CommandField();
cmd.ShowDeleteButton = true;
cmd.ShowEditButton = true;
gridViewTest.Columns.Add(cmd);
//userid
BoundField bound=new BoundField();
bound.DataField = "userid";
bound.ReadOnly = true;
bound.InsertVisible = false;
bound.SortExpression = "userid";
bound.HeaderText = "UserID";
gridViewTest.Columns.Add(bound);
//username
bound = new BoundField();
bound.DataField = "username";
bound.SortExpression = "username";
bound.HeaderText = "UserName";
bound.ReadOnly = true;
gridViewTest.Columns.Add(bound);
//sex
bound = new BoundField();
bound.DataField = "sex";
bound.SortExpression = "sex";
bound.HeaderText = "Sex";
gridViewTest.Columns.Add(bound);
//age
bound = new BoundField();
bound.DataField = "age";
bound.SortExpression = "age";
bound.HeaderText = "Age";
gridViewTest.Columns.Add(bound);
//email
bound = new BoundField();
bound.HeaderText = "E-mail";
bound.DataField = "email";
bound.SortExpression = "email";
gridViewTest.Columns.Add(bound);
//address
bound = new BoundField();
bound.HeaderText = "Address";
bound.DataField = "address";
bound.SortExpression = "address";
gridViewTest.Columns.Add(bound);
}
catch (Exception exGetData)
{
Response.Write(exGetData.Message);
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{
int i = 0;
try
{
i = sdsUsers.Insert();
if (i > 0)
{
lblMessage.Text = "OK";
}
else
{
lblMessage.Text = "wrong";
}
}
catch (Exception exBtnInsert)
{
Response.Write(exBtnInsert.Message);
}
}
}
------
.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridSourceProcedure.aspx.cs" Inherits="gridSourceProcedure" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource runat="server" ID="sdsUsers"></asp:SqlDataSource>
<asp:GridView runat="server" ID="gridViewTest" DataKeyNames="userid"></asp:GridView>
</div>
<div id="buttontest">
<table>
<tr>
<td style="width: 100px">
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label></td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lblSex" runat="server" Text="Sex"></asp:Label></td>
<td style="width: 100px">
<asp:TextBox ID="txtSex" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lblAge" runat="server" Text="Age"></asp:Label></td>
<td style="width: 100px">
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lblEmail" runat="server" Text="Email"></asp:Label></td>
<td style="width: 100px">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lblAddress" runat="server" Text="Address"></asp:Label></td>
<td style="width: 100px">
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
<asp:Button ID="btnInsert" runat="server" Text="插入数据" OnClick="btnInsert_Click" /></td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>