2012-11-16 50 views
0

我正在使用asp命令字段類型按鈕來編輯gridview行。 每當用戶更新一條記錄時,假設一行不會顯示在網格中。 ShowAllClasses()方法假設從SQL數據庫中取出最近更新的行以外的記錄。gridview使用asp的RowUpdating事件:命令字段類型按鈕

現在「更新」命令字段按鈕的行爲。

本地主機: 當我點擊按鈕它隱藏行(意味着綁定再次完成)。在這種情況下,記錄會更新一次。 (根據需要)

上部署的應用程序: 當用戶點擊該行不隱藏和用戶可以點擊按鈕多次(好像功能無法正常工作)。但是,當用戶停止點擊按鈕,經過短暫的延遲後,gridview的編輯模式消失,更新後的綁定被觸發。

糟糕的是,每次點擊按鈕時sql表都會更新。 幫我解決這個問題。

這裏是代碼我使用的內部GridView1

標記命令字段:

<asp:CommandField ButtonType="Button" HeaderText="Edit/Update" ShowEditButton="True" 
          ShowHeader="True" UpdateText="Set Class" ItemStyle-HorizontalAlign="Center" /> 

GridView的RowUpdating事件

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(); 
} 

與數據源綁定網格法

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(); 
} 

回答

0

在行更新事件結束時禁用更新鏈接按鈕。然後在行編輯事件中將控件設置爲激活。這會讓用戶不再點擊按鈕兩次。聽起來像是生產服務器的不良連接。

this.mybutton.enabled = false: 
+0

謝謝你的回覆。 我試過這個解決方案,但在這種情況下不工作。 – Hassan

+0

什麼不起作用?你有錯誤嗎? – briskovich

+0

不斷點擊CommandField按鈕會導致數據庫表中的多個更新。 – Hassan