3月 17, 2010

[ASP.NET] 檢查 Textbox 是否為空值

主要有兩個方法:
  • 使用 string.IsNullOrEmpty() 判斷是否為空值。
  • 使用 int.TryParse() 先行測試,若轉換失敗會傳回 False 表示。
範例部份:
int score =0;
bool ret= false;

/* 用來檢查 Txbox_score 是否有輸入資料 */
if (!string.IsNullOrEmpty(Txbox_score.Text))
{
/* 嘗試轉換字串存入 score 變數,執行狀況存在 ret 內 */
ret = int.TryParse(Txbox_score.Text, out score);
}

沒有留言:

張貼留言