哪位大侠帮帮忙,我的GridView更新时出问题了
前几年都是用ASP,出.NET后一直没写过程序,也接触得少了,现在学.NET 2有个新问题,怎么都没解决,烦请大侠帮忙解决解决。我有一张学生表:
LJ XH XM ……还有其它字段。
上面字段的意思依次是年级、学号、姓名,我用了一个GridView,如下:
<asp:GridView ID="myGrid" runat="server" AllowPaging="True" OnSelectedIndexChanged="myGrid_SelectedIndexChanged" OnPageIndexChanging="myGrid_PageIndexChanging" AutoGenerateColumns="False" OnRowEditing="myGrid_RowEditing" OnRowUpdating="myGrid_RowUpdating" PageSize="20" OnRowCancelingEdit="myGrid_RowCancelingEdit" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
<Columns>
<asp:BoundField DataField="LJ" HeaderText="年级" />
<asp:BoundField DataField="XH" HeaderText="学号" ReadOnly="True" />
<asp:BoundField DataField="XM" HeaderText="姓名" />
</Columns>
</asp:GridView>
myGrid_RowUpdating中有几个Response是我用来输出测试的,现在问题是点编辑时,相应行变成编辑状态,比如我把年级由2005改成2006,点更新时却发现读取的控件值还是2005,XM字段也一样的。应该是((TextBox)(myGrid.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()这个有问题吧。
我显示的时候因为年级前面还有一行是编辑、删除的列,所以第一行年级就从Cell[1]开始,这个没问题,请大家帮忙看看:
在后台Code文件中代码如下:
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;
using System.Data.OleDb;
public partial class GridView : System.Web.UI.Page
{
private string strConn = ConfigurationManager.ConnectionStrings["CONNECTACCESS"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
BindGridView();
}
protected void myGrid_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void myGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
myGrid.PageIndex = e.NewPageIndex;
BindGridView();
}
protected void myGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
myGrid.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void myGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow myGVR = myGrid.Rows[myGrid.EditIndex];
OleDbConnection myConn = new OleDbConnection(strConn);
string strSQL = "UPDATE STUREG SET LJ='"
+ ((TextBox)(myGrid.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',XM='"
+ ((TextBox)(myGrid.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' WHERE XH='"
+ myGrid.DataKeys[e.RowIndex].Value.ToString() + "'";
OleDbCommand myCMD;
try
{
myConn.Open();
myCMD = new OleDbCommand(strSQL, myConn);
myCMD.ExecuteNonQuery();
}
catch (OleDbException myOLEDBe)
{
Response.Write(myOLEDBe.Message.ToString());
}
finally
{
Response.Write("<br>" + strSQL);
myConn.Close();
myGrid.EditIndex = -1;
BindGridView();
}
}
protected void myGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
myGrid.EditIndex = -1;
BindGridView();
}
private void BindGridView()
{
OleDbConnection myConn = new OleDbConnection(strConn);
string strSQL = "SELECT * FROM STUREG ORDER BY LJ,XH";
OleDbDataAdapter myDA;
DataSet myDS = new DataSet();
try
{
myConn.Open();
myDA = new OleDbDataAdapter(strSQL, myConn);
myDA.Fill(myDS, "STU");
myGrid.DataSource = myDS.Tables["STU"];
myGrid.DataKeyNames = new string[] { "XH" };
myGrid.DataBind();
}
catch (OleDbException myOLEDBe)
{
Response.Write(myOLEDBe.Message.ToString());
}
finally
{
myConn.Close();
}
}
}
--------------------编程问答--------------------
protected void Page_Load(object sender, EventArgs e)
{
BindGridView();
}
这得问题吧,每次更新的时候都刷页面了,重新绑定了,当然是原来的值啊
写成
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
--------------------编程问答-------------------- 第一、看看数据库里面的值被修改了吗?
第二、重新绑定有出问题吗? --------------------编程问答-------------------- 应该就是一楼所说的原因
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
--------------------编程问答-------------------- ding --------------------编程问答-------------------- 没有修改。我昨天的代码,今天打开还是一样的,数据库没变。
而且连Response.Write输出的参数都是原来的值没有变呀
还请大家再看看,呵呵,我也搞不懂了,难道跟数据库有关,我先试试重建一个简单的表看看了 --------------------编程问答-------------------- 你把Page_Load的方法改了,还是那样??? --------------------编程问答-------------------- 呵呵,真是一楼说的问题,我粗心了,知道为什么了。
回传时重新Bind又把数据恢复了
Thank You Very Much!
结贴
补充:.NET技术 , ASP.NET