FormView
出自ProgWiki
FormView,System.Web.UI.WebControls.FormView
目錄 |
FAQ
如何在DataBinding或DataBound事件中取得資料
protected void FormView1_DataBound(object sender, EventArgs e) { DataRowView RowView = FormView1.DataItem as DataRowView; if (RowView != null) { this.ID.Value = RowView["ID"].ToString(); } }
如何自SqlDataSource取回Insert後的傳回值
- 參照:DataSource 取出 Output Pamameter 的值
- 必須使用SqlDataSource的Inserted事件
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e) { this.ID.Value = e.Command.Parameters["@ID"].Value.ToString(); }
關於使用OnItemInserted或OnItemUpdated事件去檢查資料是否成功寫入資料庫
- 判斷 e.AffectedRows 是否大於 0
- 如果有使用SQL預存程序的話,要在資料Insert或Update之前,做SET NOCOUNT OFF;,不然不會計數被影響的資料筆數
- 如果要在資料儲存後,仍保持表單畫面不切換到ReadOnly模式
- 在OnItemInserted事件裡使用e.KeepInInsertMode = true;
- 在OnItemUpdated事件裡使用e.KeepInEditMode = true;
關於Insert或Edit模式下的取消按鈕
protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e) { if (e.CommandName == "Cancel") { if (this.FormView1.CurrentMode == FormViewMode.Insert) { //Insert模式下的取消按鈕 } else if (this.FormView1.CurrentMode == FormViewMode.Edit) { //Edit模式下的取消按鈕 } } }