判斷字串是否為Null或空白
假設我們要做個簡單的成績輸入判斷及格與否
protected void engBtn_Click(object sender, EventArgs e)
{
int score = 0;
bool result;
//判斷TextBox中內容是否為Null或空白
if (!string.IsNullOrEmpty(engTXT.Text))
{
//將成績由字串轉為int。成功回傳true;失敗回傳false
result = int.TryParse(engTXT.Text, out score);
//字串轉int成功
if(result)
{
//判斷成績是否高於60分
if (score >= 60)
resultTXT.Text = "及格";
else
resultTXT.Text = "加油,好嗎?";
}
//字串轉int失敗
else
{
Response.Write("<script>alert('成績輸入框只能輸入數字哦~~~')</script>");
}
}
else
{
Response.Write("<script>alert('記得輸入成績在按送出哦~~~')</script>");
}
}