我就是实现一个简单的gridview编辑功能,在网上看了很多都是这样写的,不知道为什么我点更新之后根本没有修改成功,追踪((TextBox)(row.Cells[1].Controls[0])).Text值没有变化。请高手指点一下,谢谢!
aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Basic_test" %>
<!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">
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="price_no" HeaderText="价格单号" SortExpression="价格单号"/>
<asp:BoundField DataField="wgt_prc" HeaderText="单价" SortExpression="单价" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
CS代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Basic_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//初始化界面
initcomponent();
}
void initcomponent()
{
DataTable dt = BLL.Face.saleprice().Tables[0];//得到一个table的,没有问题
Session["TaskTable"] = dt;
BindData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
BLL.SalePrice.Delete(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value));
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)Session["TaskTable"];
//Update the values.
GridViewRow row = GridView1.Rows[e.RowIndex];
dt.Rows[GridView1.EditIndex]["wgt_prc"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
//Reset the edit index.
GridView1.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
private void BindData()
{
GridView1.DataSource = Session["TaskTable"];
GridView1.DataBind();
}