我写代码绑定的数据库,在点击“编辑”按钮后,输入了新数据,但更新时,取出的还是老数据,不知为何,代码如下:protected void GV1DataBing()
{
SqlDataAdapter mxda = new SqlDataAdapter("select * from gssgmx where gssgid = " + strSgid, conn);
DataSet mxds = new DataSet();
mxda.Fill(mxds);
GridView1.DataSource = mxds;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
}protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string sgsl = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string strBz = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string sqlStr = "update gssgmx set id='" + sgsl + "',bz='" + strBz + "' where id=" + ID + "";
SqlCommand comm = new SqlCommand(sqlStr, conn);
comm.ExecuteNonQuery();
GridView1.EditIndex = -1;
GV1DataBing();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GV1DataBing();
}
就在RowUpdating()中,取出的都是老数据,不知为何?谁知道,帮下忙?在线等待!!!谢谢!
{
SqlDataAdapter mxda = new SqlDataAdapter("select * from gssgmx where gssgid = " + strSgid, conn);
DataSet mxds = new DataSet();
mxda.Fill(mxds);
GridView1.DataSource = mxds;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
}protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string sgsl = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string strBz = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string sqlStr = "update gssgmx set id='" + sgsl + "',bz='" + strBz + "' where id=" + ID + "";
SqlCommand comm = new SqlCommand(sqlStr, conn);
comm.ExecuteNonQuery();
GridView1.EditIndex = -1;
GV1DataBing();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GV1DataBing();
}
就在RowUpdating()中,取出的都是老数据,不知为何?谁知道,帮下忙?在线等待!!!谢谢!
在每次页面加载的时候都执行了吧,所以每次都是数据库中的数据。
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
GV1DataBing()
}
}试试行不?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}刚才少写个!号,歉意!
更新用id , 查詢用gssgid, 是不是新的 id更gssgid不一樣啊?