自訂欄中的資料更新
假設資料庫中有一個"許可權"欄位,值為0代表未審核使用者,為1代表一般使用者,為9則代表管理使用者。根據前面所說的自訂欄的辦法,通過對DropListDown的綁定,在網頁中顯示許可權為 "管理員",而不是顯示數字9。問題產生了,如果我們調整使用者權限的話,比如把一般使用者更改為管理員,在編輯模版中的使用者權限的下拉式清單,如何把它的值返回給資料來源來完成更新操作。
我們在EditItemTemplate中設定的DropListDown控制項,必須選中了 Two Way DataBinding,也就是資料雙向幫定,這樣才能返回資料。前面我們談到,在GridView中,事件不是單個的,是兩個,一個是發生前,一個是發生後,因為我們需要在資料更新前把下拉式清單的許可權值傳送出去,所以我們需要對GridView1_RowUpdating 進行編碼,編碼如下:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//當前編輯的是哪行?
int index = GridView1.EditIndex;
//取得當前編輯行的GridViewRow對象
GridViewRow gvr = GridView1.Rows[index];
//在當前行中,尋找DropListDown控制項
DropDownList dp = (DropDownList)gvr.FindControl("editdrop");
/將DropListDown的值賦給NewValues集合中的許可權欄位。
e.NewValues["rights"] = dp.SelectedValue;
}
2、RowDataBound事件
在建立gridView控制項時,必須先為GridView的每一行建立一個GridViewRow對象,建立每一行時,將引發一個RowCreated事件;當行建立完畢,每一行GridViewRow就要綁定資料來源中的資料,當綁定完成後,將引發RowDataBound事件。如果說我們可以利用 RowCreated事件來控制每一行綁定的控制項,那麼我們同樣可以利用RowDataBound事件來控制每一行繫結資料,也就是讓資料如何呈現給大家。
還舉同樣的例子,在資料表中,存在性別列,上面我們用DropListDown控制項的DataBounding來表示出了中文的性別,但是畢竟不太美觀,我們現在可以利用Label控制項和RowDataBound事件來實現完美的中文性別顯示。RowDataBound,
首先,還是把性別列,設定為模板列,並添加一個Label控制項,將Label控制項綁定到資料來源的性別段,然後我們在GridView控制項屬性的事件列表中雙擊RowDataBound,產生如下事件:
Example:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判斷當前行是否是資料行
if (e.Row.RowType == DataControlRowType.DataRow)
{ //用FindControl方法找到模板中的Label控制項
Label lb1= (Label)e.Row.FindControl("Label1");
//因為RowDataBound是發生在資料繫結之後,所以我們可以
//判斷Label繫結資料,如果是True,就更改其text屬性為男
if (lb1.Text== "True")
lb1.Text = "男";
else
lb1.Text = "female";
}
}
3、RowType
RowType 可以確定GridView中行的類型,RowType是玫舉變數DataControlRowType中的一個值。RowType可以取值包括 DataRow、Footer、Header、EmptyDataRow、Pager、Separator。很多時候,我們需要判斷當前是否是資料行,通過如下代碼來進行判斷 :
if (e.Row.RowType == DataControlRowType.DataRow)
4、RowDeleting和RowDeleted事件
RowDeleting發生在刪除資料之前,RowDeleted發生在刪除資料之後。
使用RowDeleting事件,可以在真正刪除前再次確認是否刪除,可以通過設定GridViewDeleteEventArgs.Cancel=True來取消刪除;也可以用於判斷當前資料庫記錄數,如果只剩一條記錄且資料庫不可為空則提示並取消刪除操作。
使用RowDeleted事件,可以在刪除後,通過GridViewDeletedEventArgs的Exception屬性判斷刪除過程中是否產生異常,如無異常,則可以顯示類似於” 1 Records deleted” 之類的提示資訊。
Example:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//取得當前行號,並取得當前行的GridViewRow對象
int index=e.RowIndex ;
GridViewRow gvr=GridView1.Rows[index];
//取得當前行第二個儲存格中的文字
str1 = gvr.Cells[1].Text;
//進行提示
Message.Text ="您將刪除一個使用者,其姓名為"+str1 ;
}
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
//如果沒有產生異常,則提示成功刪除,否則提示刪除失敗
if (e.Exception == null)
Message.Text += "<br>您成功刪除了"+str1 ;
else
Message.Text += "刪除失敗,請聯絡管理員";
}
5、RowEditing事件
在GridView中的行進入編輯模式之前,引發RowEditing事件,如果您需要在編輯記錄前進行某些預先處理,可以在這裡操作。如果想取消對當前行的編輯,可以把GridViewEditEventArgs 對象的 Cancel 屬性設定為 true即可。
Example:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//用NewEidIndex取得當前編輯的行號,然後擷取gridviewrow對象
GridViewRow gvr = GridView1.Rows[e.NewEditIndex];
//判斷,如果當前編輯行姓名欄為admin使用者,則取消對當前行的編輯
if (gvr.Cells[1].Text =="admin")
e.Cancel = true;
}
6、RowUpdating和RowUpdated事件
RowUpdating事件發生在更新資料來源之前,RowUpdated發生在更新資料來源之後。
我們可以在記錄更新前利用RowUpdating做一些預先處理工作,比如修改密碼時,因為密碼在資料庫中不是明文儲存,進行了hash,所以在更新密碼前,應該產生其hash值,再進行更新操作。RowUpdated則可以檢驗更新是否成功。
Example:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvr = GridView1.Rows[GridView1 .EditIndex ];
//尋找輸入密碼的控制項
TextBox tb1 = (TextBox)gvr.FindControl("tb_password");
//將此控制項中的文本hash後,把password存入NewValues這個字典中
e.NewValues ["password"] =tb1.Text .GetHashCode().ToString () ;
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
//如無異常,則更新成功
if (e.Exception == null)
Message.Text += "更新成功!";
}
7、Keys、OldValues、NewValues集合
這三個集合中的每一項都是DictionaryEntry類型的對象,我們可以用DictionaryEntry.Key來確定一個項的欄位名稱,用DictionaryEntry.Value來確定某項的值。
在上面的例子中,為了把密碼明文加密後再存入資料庫,我們利用了NewValues欄位,重新設定key為password的項的值。為了保證安全性,我們在更新資料前對NewValues中的所有值進行html編碼:
Example1:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//遍曆NewValues,取得其中每一對DictionaryEntry對象
foreach (DictionaryEntry de in e.NewValues)
//de.key就是欄位名,如果此處單獨更新某欄位的話,也可以直接填寫欄位名,//比如 e.NewValues[“password”]
e.NewValues[de.Key] = Server.HtmlEncode(de.Value.ToString());
}
Example2:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//分別利用Keys、OldValues、NewValues取得主鍵名、未經處理資料和更新後資料
Message .Text = e.Keys["username"] + "的email地址從" + e.OldValues["email"] + "變更為" + e.NewValues["email"];
}