好的,我需要檢查15個文本框中只有4個是空的,但沒有運氣。我想:檢查某些文本框是否爲空
if txtbox1.text = "" then
lblerror1.visible=true
exitsub
else
bla bla
end if
但是,左錯誤文本,並沒有看到進入文本框中的文本的用戶,所以我看了一下,發現string.isnullorwhitespace(string.value)... 好,這沒告訴我如何使用它,所以我搜索了更多,發現這個: if string.isnullorwhitespace(textbox.text)then ..
好吧,這是它,這裏是結果。現在,如果我只能得到一個for-next或do-while只需要檢查4個文本文件,我需要檢查而不是所有的文本框。
ASPX頁面代碼:
<asp:Label ID="lblerror" runat="server" Text="Page error" Visible="false" forecolor="red"/><br />
<asp:TextBox ID="txtName" runat="server" Width="100px" /><asp:Label ID="nameblankerror" runat='server' Text="cannot be blank" ForeColor="Red" Visible="false" /><br />
<asp:TextBox ID="txtcompname" runat="server" Width="100px" /><asp:Label ID="compblankerror" runat='server' Text="cannot be blank" ForeColor="Red" Visible="false" /><br />
<asp:Button ID="btnSubmit" runat="server" Text="submit" /><br />
<asp:Label ID="lbl1" runat="server" Visible="true" Text="TextBox 1: " /><asp:label ID="lblname" runat="server" /><br />
<asp:Label ID="lbl2" runat="server" Visible="true" Text="TextBox 2: " /><asp:label ID="lblCompName" runat="server" />
和後端代碼:
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
'test to see if certain the fields are blank
Dim str1 As String = txtName.Text
Dim str2 As String = txtcompname.Text
Dim CatchMe As Integer = 0
If String.IsNullOrWhiteSpace(txtName.Text) Then
nameblankerror.Visible = True
CatchMe += 1
Else
nameblankerror.Visible = False
lblname.text = str1.Trim()
CatchMe += 0
End If
If String.IsNullOrWhiteSpace(txtcompname.Text) Then
compblankerror.Visible = True
CatchMe += 1
Else
compblankerror.Visible = False
lblCompName.Text = str2.Trim()
CatchMe += 0
End If
If CatchMe = 0 Then
'do something like process SQL Command
lblerror.Visible = False
Exit Sub
ElseIf CatchMe <> 0 Then
'return to page and process nothing
lblerror.Visible = True
Exit Sub
End If
End Sub
原來如此。一個簡單的,易於檢查某些文本框出來的一堆。 就像我上面說過的,如果我能弄清楚如何只檢查某些文本框而不是所有的文本框,那麼縮短代碼會很好。我放入一個catchme,這樣如果填充了一個盒子,它不會向用戶顯示他們也需要填充該盒子(在Error中),但會捕獲其他空的文本框。 說清楚,如果我有15個文本框,但只關心其中4個不是空的,這是我爲檢查做的。我不這樣做每個文本框,因爲它是不需要的
你知道你需要驗證的文本框的id嗎? – Gaurav
這是問題1:它們從id =「txtb1到15」,id是1,2,4,8。但是如果我將1,2,4,8更改爲1-4,那麼我可以爲'txtb1 to txt4 bla bla next txtb'做一個'或類似的事情。 – d4d4u1
那麼你想檢查的15箇中有哪4個? –