如何用高效的代码验证代码后面的5个文本框?这就是我尝试的,但它给了我错误



我的代码比这更复杂,但为了保持简单,只发布了5个按钮,当我点击一个按钮时,我想做一些逻辑来验证每个文本框。但是,只需更改代码后面显示的ID,就可以在循环中完成此操作。如有任何帮助,将不胜感激

.aspx

<asp:TextBox ID="txtEmployeeID1" runat="server" Width="95px"></asp:TextBox>
<asp:TextBox ID="txtEmployeeID2" runat="server" Width="95px"></asp:TextBox>
<asp:TextBox ID="txtEmployeeID3" runat="server" Width="95px"></asp:TextBox>
<asp:TextBox ID="txtEmployeeID4" runat="server" Width="95px"></asp:TextBox>
<asp:TextBox ID="txtEmployeeID5" runat="server" Width="95px"></asp:TextBox>

.aspx.vb

 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Validate emptext
    Dim b As Integer = 1
    Dim employeeid As TextBox
    Do While b < 6
        employeeid.ID = "EmployeeID" & "1" '(b.ToString())
        Dim str As String = employeeid.Text
        b = b + 1
    Loop
End Sub

创建一个TextBox数组,并在该数组元素上循环

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim txts = {txtEmployeeID1, txtEmployeeID2, txtEmployeeID3, txtEmployeeID4, txtEmployeeID5}
    ForEach t in txts
        Dim str As String = t.Text
        .... do you checks here ...
    Loop
End Sub

创建一个单独的方法来处理验证,并从按钮单击事件中调用它。

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.ValidateEmployeeID(Me.txtEmployeeID1)
    Me.ValidateEmployeeID(Me.txtEmployeeID2)
    Me.ValidateEmployeeID(Me.txtEmployeeID3)
    Me.ValidateEmployeeID(Me.txtEmployeeID4)
    Me.ValidateEmployeeID(Me.txtEmployeeID5)
End Sub
Private Sub ValidateEmployeeID(textBox As TextBox)
    ' common logic here
End Sub

这里是我在正则表达式上使用的一些代码。您需要根据自己的需要进行轻微的修改,但这通常会派上用场。用VB.NET为ASP.NET 编写

首先使用此函数获取任何控件内的文本框列表。

''' <summary>
''' get a list of all textboxes nested under ctrlParent and its children.
''' </summary>
''' <param name="ctrlParent"></param>
''' <param name="getReadOnlyTxts">
''' If this is set to false then textboxes with the class "txtReadOnly" will be ignored.
''' Otherwise all textboxes can be returned in this funciton
''' </param>
''' <returns></returns>
''' <remarks>
''' this is a recursive function that calls itself.
''' </remarks>
Public Function GetList_Textboxes(ByVal ctrlParent As Control, ByRef getReadOnlyTxts As Boolean) As List(Of TextBox)
    Dim ctrl As Control
    Dim txt As TextBox
    Dim lstTxtbox As New List(Of TextBox)
    For Each ctrl In ctrlParent.Controls
        If TypeOf ctrl Is TextBox Then
            txt = TryCast(ctrl, TextBox)
            If getReadOnlyTxts Then
                'get read only texts is true, so add the textbox without checking
                lstTxtbox.Add(txt)
            Else
                'get read only textboxes is not true, so check to make sure
                'this textbox is not readonly before adding
                If Not InStr(txt.CssClass, "txtReadOnly") > 0 Then
                    lstTxtbox.Add(txt)
                End If
            End If
        End If
        ' If the control has children, recursively call this function
        If ctrl.HasControls Then
            lstTxtbox.AddRange(GetList_Textboxes(ctrl, getReadOnlyTxts))
        End If
    Next
    Return lstTxtbox
End Function

然后将该函数的结果列表传递给这个:

''' <summary>
''' send a list of textboxes to validate
''' if the control is empty then its backcolor will be changed and valid will be set to false
''' </summary>
''' <param name="lstTxtboxs"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Validate_Textboxes(ByRef lstTxtboxs As List(Of TextBox)) As Boolean
    Dim valid As Boolean = True
    For Each txtbox As TextBox In lstTxtboxs
        If Trim(txtbox.Text) = "" Then
            txtbox.BackColor = _ctrlColorError
            valid = False
        Else
            txtbox.BackColor = _ctrlColorNormal
        End If
    Next
    Return valid
End Function

您只需要编写一次上面的两个方法,并在类文件或其他文件中公开它们。从那时起,如果您的文本框为空,您只需执行以下操作即可验证:

Dim lstRequiredTxtbxs As New List(Of TextBox)
lstRequiredTxtbxs = GetList_Textboxes(ID_ofControlToLookForTextboxesUnder, False)
Dim areTextboxesValid as Boolean = Validate_Textboxes(lstRequiredTxtbxs)

例如1:

Dim emptyTextBoxes =
    From txt In Me.Controls.OfType(Of TextBox)()
    Where txt.Text.Length = 0
    Select txt.Name
If emptyTextBoxes.Any Then
    MessageBox.Show(String.Format("Please fill following textboxes: {0}",
                    String.Join(",", emptyTextBoxes)))
End If

例如2:

void ProcessTextBoxes(Control parent) {
   foreach(Control child in parent.Controls) {
      TextBox textBox = child as TextBox;
      if (textBox != null) // type match
          ProcessTextBox(textBox); //define some method to work with individual text box
      else
          ProcessTextBoxes(parent); //recursive
   } //loop
}

最新更新