我用GridView 对某一项进行编辑时,数据库不能获取编辑后的值,是怎么回事,谁能帮帮我呢?
我用GridView 对某一项进行编辑时,数据库不能获取编辑后的值,是怎么回事,谁能帮帮我呢?
问一下下面代码里有没有错误啊!
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.SqlClient;
public partial class backmanage_memberscoremanage : System.Web.UI.Page
{
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings[ "conn "]);
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (Session[ "admin "] == null)
{
Server.Transfer( "backlogin.aspx ");
}
dsdatabind();
}
protected void dsdatabind()
{
strcon.Open();
string str = "SELECT id,username,score,address,tel from tb_users order by score desc ";
SqlDataAdapter datap = new SqlDataAdapter(str, strcon);
datap.Fill(ds);
GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "id " };
GridView1.DataBind();
strcon.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
strcon.Open();
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlCommand comm = new SqlCommand( "delete from tb_users where id= " + id, strcon);
comm.ExecuteNonQuery();
strcon.Close();
dsdatabind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
dsdatabind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
dsdatabind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
strcon.Open();
string uid = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = this.GridView1.Rows[e.RowIndex];
string str = "update tb_users set score= ' " + ((TextBox)(row.Cells[2].Controls[0])).Text.ToString() + " ' where id= ' " + uid+ " ' ";
SqlCommand goodsin = new SqlCommand(str, strcon);
goodsin.ExecuteNonQuery();
strcon.Close();
GridView1.EditIndex = -1;
dsdatabind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
dsdatabind();
}
}
[解决办法]
Page_Load事件中添加
if(!Page.IsPostBack)
{
//你的代码
}