【发布时间】:2020-01-13 05:43:43
【问题描述】:
我正在使用 webforms 上的 asp.net gridview 并使用 GridView OnRowCommand 事件的 Gridview RowCommand 方法在 TemplateField 中进行按钮查看、编辑和更新。
if (e.CommandName == "EditContract")
{
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;
int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
gvContract.SelectRow(rowIndex);
using (SqlCommand cmd = new SqlCommand("spContractEdit", myObj.DbConnect()))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@RoleName", SqlDbType.Char).Value = Session["RoleName"];
cmd.Parameters.Add("@UnitName", SqlDbType.Char).Value = Session["UnitName"];
cmd.Parameters.Add("@ContrSerialNo", SqlDbType.Int).Value = SerialNo;
dAdapter = new SqlDataAdapter(cmd);
DataTable DtContract = new DataTable();
dAdapter.Fill(DtContract);
}
if (e.CommandName == "UpdateContract")
{
lblMessage.Text = "";
lblFile.Text = "";
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;
using (SqlCommand cmd = new SqlCommand("spContractUpdate", myObj.DbConnect()))
{
cmd.CommandType = CommandType.StoredProcedure;
}
}
第一次加载页面时,此代码运行良好,但之后出现 2 个问题。即 1. 当页面第一次加载时它正在编辑和更新数据,但是当我尝试编辑同一行或任何其他行时它不会。我知道这是因为我在 Page_Load 事件上定义了 !Page.IsPostback 方法,但我不知道如何解决这种情况。 2.问题是如何限制gridview行只更新被选中行的数据?
请给我一个解决方案。
【问题讨论】:
标签: asp.net gridview webforms rowcommand