close

這裡的文章是我個人的備份。
如果要閱讀原文,請到我的主網站:http://www.dotblogs.com.tw/mis2000lab/

 

 


本篇文章的原始出處(原文),位於:[習題] ListView 資料更新前,先檢查欄位有無空白?

 

http://www.dotblogs.com.tw/mis2000lab/archive/2009/11/13/listview_itemupdating.aspx

 

=====================================================

 

 

 

 

這個範例是在微軟MSDN網站上看見的,因為覺得很好用,所以幫自己作個記錄

我只是「轉貼」......

 

 資料來源 & 完整範例: http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.listview.itemupdating.aspx

 

 

Q:  當 ListView在編輯模式裡面,我按下「更新(Update)」按鈕

      就會啟動 ListView的 _ItemUpdating()事件。

      我希望在資料更新前,逐一檢查每一個欄位是否空白?

    如果有空白,就「取消」這次更新的動作。

 

 

A:  範例如下

VB版程式:

Sub ContactsListView_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)

    ' Cancel the update operation if any of the fields is empty
    ' or null.
    For Each de As DictionaryEntry In e.NewValues
         ' Check if the value is null or empty
         If de.Value Is Nothing OrElse de.Value.ToString().Trim().Length = 0 Then
              Message.Text = "Cannot set a field to an empty value."
              e.Cancel = True
         End If
    Next

    ' Convert the e-mail address to lowercase.
    Dim emailValue As String = e.NewValues("EmailAddress").ToString()   
    e.NewValues("EmailAddress") = emailValue.ToLower()

End Sub
..........................................................................................................................................

 

C#版程式:

  void ContactsListView_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
  {
      // Cancel the update operation if any of the fields is empty
      // or null.
      foreach (DictionaryEntry de in e.NewValues)
      {
          // Check if the value is null or empty.
          if (de.Value == null || de.Value.ToString().Trim().Length == 0)
          {
                Message.Text = "Cannot set a field to an empty value.";
                e.Cancel = true;
          }
      }

      // Convert the e-mail address to lowercase.
      String emailValue = e.NewValues["EmailAddress"].ToString();
      e.NewValues["EmailAddress"] = emailValue.ToLower();
  }
 

資料來源 & 完整範例: http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.listview.itemupdating.aspx

 

 

 

2009/11/25補充:

上述的程式碼,如果要寫在「ListView的新增」功能裡面 (例如 ContactsListView_ItemInserting()事件)

請將程式裡面 for迴圈 的 e.NewValues

修改成  e.Values即可

 

 


 

 

......  寄信給我    mis2000lab (at) 雅虎.com.台灣 ................................................................................................................
ASP.NET專題實務  (文魁出版,VB版 P8187 / C#版P09027

.............................................................................................................. 寄信給我    mis2000lab (at) 雅虎.com.台灣 ........

arrow
arrow
    全站熱搜

    MIS2000 Lab 發表在 痞客邦 留言(0) 人氣()