我正在使用 asp 命令字段类型按钮来编辑 gridview 行。每当用户更新记录时,假设网格中不会显示一行。ShowAllClasses() 方法假设从 sql db 中获取记录,不包括最近更新的行。
现在“更新”命令字段按钮的行为。
本地主机:当我单击按钮时,它会隐藏该行(意味着再次完成绑定)。在这种情况下,记录会更新一次。(按要求)
在已部署的应用程序上:用户单击该行时,该行不会隐藏,并且用户可以多次单击该按钮(好像功能不起作用)。但是当用户停止单击按钮时,在稍有延迟后,gridview 的编辑模式就会消失,并且会在触发更新后绑定。
不好的是每次单击按钮时 sql 表都会更新。帮助我如何解决这个问题。
这是我正在使用的代码
GridView1 内命令字段的标记:
<asp:CommandField ButtonType="Button" HeaderText="Edit/Update" ShowEditButton="True"
ShowHeader="True" UpdateText="Set Class" ItemStyle-HorizontalAlign="Center" />
GridView 行更新事件:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
lblError.Text = string.Empty;
int iTutorID = Convert.ToInt32(Session["EID"]);
DropDownList ddlTime = GridView1.Rows[e.RowIndex].FindControl("ddlClassTime") as DropDownList;
string sClassTime = ddlTime.SelectedItem.Text;
HiddenField hf = GridView1.Rows[e.RowIndex].FindControl("sIdHidden") as HiddenField;
int iSID = Convert.ToInt32(hf.Value);
hf = GridView1.Rows[e.RowIndex].FindControl("cIdHidden") as HiddenField;
int iCID = Convert.ToInt32(hf.Value);
hf = GridView1.Rows[e.RowIndex].FindControl("hfClassTime") as HiddenField;
string sOriginalClassTime = hf.Value.ToString();
if (sOriginalClassTime.Length < 8)
sOriginalClassTime = "0" + sOriginalClassTime;
DropDownList ddlDate = GridView1.Rows[e.RowIndex].FindControl("ddlClassDate") as DropDownList;
DateTime dNewDate = Convert.ToDateTime(ddlDate.SelectedValue);
Label lblLesson = GridView1.Rows[e.RowIndex].FindControl("lblLesson") as Label;
string sLesson = lblLesson.Text;
DateTime dClassDate = Convert.ToDateTime(ddlAdvDay.SelectedValue);
//cond: same date and same time set
if (sOriginalClassTime == sClassTime && dNewDate == dClassDate.Date)
{
//show error
lblError.Text = "Same class date and time cannot be set as Advanced class";
return;
}
lblError.Text = string.Empty;
BLClass blClass = new BLClass();
//1. insert in makeup table today's class
//2. insert in classdetails table
//1. insert in makeup table with today's date
blClass.InsertAdvancedClass(iTutorID, iSID, iCID, dClassDate, dNewDate);
//2. insert in classdetails table
blClass.InsertClassDetails(iTutorID, iSID, iCID, dNewDate, sClassTime, "Advanced", sLesson);
GridView1.EditIndex = -1;
ShowAllClasses();
}
使用 DataSource 绑定网格的方法:
private void ShowAllClasses()
{
lblError.Text = string.Empty;
int iTutorID = Convert.ToInt32(Session["EID"]);
BLClass blClass = new BLClass();
DateTime dClassDate = Convert.ToDateTime(ddlAdvDay.SelectedValue);
DataTable dtClass = blClass.GetAdvancedClasses(iTutorID, dClassDate.Date);
//temp method for date display format
FixDateFormat(dtClass);
dtClass.AcceptChanges();
GridView1.DataSource = dtClass;
GridView1.DataBind();
}