鉴于之前我转载了GridView的解释和更新删除操作的步骤说明,并没有融入自己的想法和认识。下面是我自己整理的关于GridView的更新删除方案,希望大家能够互相学习。这里的方法暂时不涉及到模板页,而且用的是微软自带的GridView的三种事件:GridView1_RowEditing(编辑)、GridView1_RowUpdating(更新)、GridView1_RowCancelingEdit(取消编辑)。
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewCommon.aspx.cs" Inherits="GridViewCollection.GridViewCommon" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>微软原型</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" EnableViewState="true" AllowPaging="true" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="品牌" HeaderText="品牌" SortExpression="品牌" />
<asp:BoundField DataField="价格" HeaderText="价格" SortExpression="价格" />
<asp:BoundField DataField="数量" HeaderText="数量" SortExpression="数量" />
<asp:BoundField DataField="生产地" HeaderText="生产地" SortExpression="生产地" />
<%--默认列类型--%>
<asp:CommandField CausesValidation="false" ShowEditButton="true" ShowHeader="true" HeaderText="编辑" />
<asp:CommandField CausesValidation="false" ShowDeleteButton="true" ShowHeader="true" HeaderText="删除" />
</Columns>
</asp:GridView>
<%--<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:con %>" SelectCommand="SELECT [品牌], [价格], [数量], [生产地], [id] FROM [Cloths]"></asp:SqlDataSource>--%>
</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;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace GridViewCollection
{
public partial class GridViewCommon : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GView();
}
}
private void GView()
{
string con = ConfigurationManager.ConnectionStrings["con"].ToString();
string str = "select * from Cloths";
SqlConnection sqlcon = new SqlConnection(con);
sqlcon.Open();
SqlDataAdapter sqlad = new SqlDataAdapter(str,sqlcon);
DataSet ds = new DataSet();
sqlad.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
sqlcon.Close();
}
private void ChangeRow(string str)
{
string con = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlConnection sqlcon = new SqlConnection(con);
sqlcon.Open();
SqlCommand sqlcom = new SqlCommand(str,sqlcon);
sqlcom.ExecuteNonQuery();
sqlcon.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{//执行删除
string str = "delete from Cloths where id='"+ GridView1.DataKeys[e.RowIndex].Value.ToString() +"'";
ChangeRow(str);
GView();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{//激活编辑
this.GridView1.EditIndex = e.NewEditIndex;
GView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{//执行更新
string cell1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string cell2 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string cell3 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string cell4 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string str = "update Cloths set 品牌='" + cell1 + "',价格='" + cell2 + "',数量='" + cell3 + "',生产地='" + cell4 + "' where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
ChangeRow(str);
GridView1.EditIndex = -1;
GView();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{//取消编辑
GridView1.EditIndex = -1;
GView();
}
}
}
在做这个示例之前,以为不会遇到什么问题的,毕竟已经有转载的文章可以参照,但是在自己动手做的过程中,还是会遇到各种各样千奇百怪的错误。下面是我遇到的问题以及解决方案,希望可以被大家参考。
1.关于GridView的GridView1_RowEditing(编辑)、GridView1_RowUpdating(更新)、GridView1_RowCancelingEdit(取消编辑)、GridView1_RowDeleting(删除)。编辑,更新,取消编辑这三个方法还是很简单的,对于GridView1_RowUpdating这个方法就没那么简单了。
原先我把GridView的EnableViewState设置为false(他的默认值是true),然后运行就一直提示错误。百般百度和调试,最后终于发现了EnableViewState这个的问题。原先我设为false,然后在
string cell1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();一直提示超出范围的错误,后来我改成了true就可以了。
关于EnableViewState,有点难懂,我网上找到了这么一段话:
服务器是怎么知道客户的操作的呢?比如我在文本框输入的内容,或者单击了登录按钮,服务器端是怎样得到这些信息的呢?因为没有这些信息,服务器端就无法响应客户的请求。原理就是ASP.NET引用了viewstate的机制。在服务器端保存了网页各个控件及页面的状态,这其中包括各个控件在页面上的布局,和他们各自的属性。这些值就保存在ViewState下。
关于更多EnableViewState的链接:http://www.cnblogs.com/wayfarer/archive/2004/04/25/7574.aspx
还有就是CauseValidation这一说,如果会遇到这么一种情况:
当点击“编辑”按钮以后,可以看到“更新”和“取消”按钮,“取消”按钮可以正常触发RowCancelingEdit事件,但是“更新”按钮不能触发RowUpdating事件。
相关的链接:http://www.cnblogs.com/emanlee/archive/2009/03/02/1401826.html