C# 无法更新C中文本框中的记录#
我正在尝试编辑内部网格视图。当我点击编辑按钮更新文本框中的记录时,它会显示以前的记录。请查收。我认为错误在行更新事件中C# 无法更新C中文本框中的记录#,c#,asp.net,gridview,aspxgridview,C#,Asp.net,Gridview,Aspxgridview,我正在尝试编辑内部网格视图。当我点击编辑按钮更新文本框中的记录时,它会显示以前的记录。请查收。我认为错误在行更新事件中 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { fill_grd1(); } } public void fill_grd1() { con.Open(); SqlCommand cmd = new SqlCommand(
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fill_grd1();
}
}
public void fill_grd1()
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_country_get", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
grd1.DataSource = ds;
grd1.DataBind();
}
else
{
grd1.DataSource = null;
grd1.DataBind();
}
con.Close();
}
protected void savebtn_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_country_insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@cname", txtcname.Text);
cmd.ExecuteNonQuery();
con.Close();
fill_grd1();
}
protected void grd1_RowEditing(object sender, GridViewEditEventArgs e)
{
grd1.EditIndex = e.NewEditIndex;
fill_grd1();
}
protected void grd1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(grd1.DataKeys[e.RowIndex].Value.ToString());
con.Open();
SqlCommand cmd = new SqlCommand("usp_country_delete", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@cid", id);
cmd.ExecuteNonQuery();
con.Close();
fill_grd1();
}
protected void grd1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox TB1 = (TextBox)grd1.Rows[e.RowIndex].FindControl("edittxtcname");
int id = int.Parse(grd1.DataKeys[e.RowIndex].Value.ToString());
con.Open();
SqlCommand cmd = new SqlCommand("usp_country_update", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@cid", id);
cmd.Parameters.AddWithValue("@cname", TB1.Text);
cmd.ExecuteNonQuery();
con.Close();
grd1.EditIndex = -1;
fill_grd1();
}
protected void grd1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grd1.EditIndex = -1;
fill_grd1();
}
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void grd1_RowCommand(object sender, GridViewCommandEventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_country_status_change", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@cid", e.CommandArgument);
cmd.ExecuteNonQuery();
con.Close();
fill_grd1();
}
否,我采取了-if(!IsPostBack){fill_grd1();}
grd1_RowCommand()
发生在grd1_RowCommand()
我的RowCommand()正在更改状态之前-Active到Inactive&RowCommand()正在更新记录。我应该做什么更改?所有编辑/更新/选择/插入操作都通过RowCommand。解决方法之一是使用if()
:if(e.CommandName!=“Update”)
或任何需要验证的内容将代码包装在row命令中,或者您可以将状态更改代码移动到它自己的函数中并调用该函数,例如在grd1.RowUpdated()
中以及编辑操作后需要验证状态的任何地方。