我正在尝试动态创建一个用于联系信息的表。我知道你输入你的名字,选择与客户的关系,然后填写3个电话号码。添加新行时,我正在制作它,以便至少填写其中一个数字。完成后,您可以添加新的联系人。
我在VB.Net中这样做,并希望有人能够提供帮助。
在查看代码之前,我想提一下我已经注释掉了很多行。仅仅为了代码背后的一些背景,我尝试了很多方法,并不确定我是否在正确的道路上。无论如何,这里是我进行动态验证的代码块。
tCell = New TableCell
tCell.CssClass = "dynamicTCell"
tCell.Controls.Add(tDrop)
tRow.Cells.Add(tCell)
tCell.Controls.Add(conRowRequired)
tBoxHome = New TextBox
tBoxHome.ID = "con" + conRows.ToString + "HomePhone"
'tBoxHome.ValidationGroup = "conPhoneNumbers" + conRows.ToString
'tBoxHome.CausesValidation = True
conRowRequiredHome = New RequiredFieldValidator
'conRowRequiredHome = New CustomValidator
'conRowRequiredHome.ClientValidationFunction = "ServerValidation"
conRowRequiredHome.ID = "conHomePhoneRequired" + conRows.ToString
'AddHandler conRowRequiredHome.ServerValidate, AddressOf ServerValidation
'tBoxHome = conTable.FindControl("con" + conRows.ToString + "HomePhone")
conRowRequiredHome.ControlToValidate = tBoxHome.ID
'conRowRequiredHome.ValidationGroup = "conPhoneNumbers"
conRowRequiredHome.Display = ValidatorDisplay.Dynamic
conRowRequiredHome.ErrorMessage = "*"
tCell = New TableCell
tCell.CssClass = "dynamicTCell"
tCell.Controls.Add(tBoxHome)
tCell.Controls.Add(conRowRequiredHome)
tRow.Cells.Add(tCell)
'conRowRequiredHome.IsValid = False
tBoxWork = New TextBox
tBoxWork.ID = "con" + conRows.ToString + "WorkPhone"
'tBoxWork.ValidationGroup = "conPhoneNumbers" + conRows.ToString
'tBoxWork.CausesValidation = True
'conRowRequiredWork = New CustomValidator
conRowRequiredWork = New RequiredFieldValidator
'conRowRequiredWork.ClientValidationFunction = "ServerValidation"
conRowRequiredWork.ID = "conWorkPhoneRequired" + conRows.ToString
'tBoxWork = conTable.FindControl("con" + conRows.ToString + "WorkPhone")
conRowRequiredWork.ControlToValidate = tBoxWork.ID
'conRowRequiredWork.ValidationGroup = "conPhoneNumbers"
conRowRequiredWork.Display = ValidatorDisplay.Dynamic
conRowRequiredWork.ErrorMessage = "*"
tCell = New TableCell
tCell.CssClass = "dynamicTCell"
tCell.Controls.Add(tBoxWork)
tRow.Cells.Add(tCell)
'conRowRequiredWork.IsValid = False
tCell.Controls.Add(conRowRequiredWork)
tBoxCell = New TextBox
tBoxCell.ID = "con" + conRows.ToString + "CellPhone"
'tBoxCell.ValidationGroup = "conPhoneNumbers" + conRows.ToString
' tBoxCell.CausesValidation = True
conRowRequiredCell = New RequiredFieldValidator
'conRowRequiredCell = New CustomValidator
'conRowRequiredCell.ClientValidationFunction = "ServerValidation"
conRowRequiredCell.ID = "conCellPhoneRequired" + conRows.ToString
'tBoxCell = conTable.FindControl("con" + conRows.ToString + "CellPhone")
conRowRequiredCell.ControlToValidate = tBoxCell.ID
'conRowRequiredCell.OnServerValidate = "ServerValidation"
'conRowRequiredCell.ValidationGroup = "conPhoneNumbers"
conRowRequiredCell.Display = ValidatorDisplay.Dynamic
conRowRequiredCell.ErrorMessage = "*"
tCell = New TableCell
tCell.CssClass = "dynamicTextBoxBRRounded"
tCell.Controls.Add(tBoxCell)
tRow.Cells.Add(tCell)
tCell.Controls.Add(conRowRequiredCell)
'Dim flag As Boolean
'If tBoxWork IsNot Nothing AndAlso tBoxCell IsNot Nothing AndAlso tBoxHome IsNot Nothing Then
' 'If (tBoxHome.Text = "" And tBoxWork.Text = "" And tBoxCell.Text = "") Then
' 'args.IsValid = False
' If (tBoxHome.Text <> "" OrElse tBoxCell.Text <> "" OrElse tBoxWork.Text <> "") Then
' flag = True
' End If
'End If
我用于尝试conRoeRequiredCell.ClientValidationFunction =“ServerValidation”的函数如下所示。我知道它说Sub Class,但我也试过它作为一个功能,没有优势。
'Protected Sub ServerValidation(ByVal source As Object, ByVal args As ServerValidateEventArgs)
' args.IsValid = False
' Dim conTable As Table = DirectCast(PlaceHolderSiblings.FindControl("conTable"), Table)
' Dim tBoxWork As TextBox = conTable.FindControl("cony" + Session("conRows").ToString + "WorkPhone")
' Dim tBoxCell As TextBox = conTable.FindControl("con" + Session("conRows").ToString + "CellPhone")
' Dim tBoxHome As TextBox = conTable.FindControl("con" + Session("conRows").ToString + "HomePhone")
' If tBoxWork IsNot Nothing AndAlso tBoxCell IsNot Nothing AndAlso tBoxHome IsNot Nothing Then
' 'If (tBoxHome.Text = "" And tBoxWork.Text = "" And tBoxCell.Text = "") Then
' 'args.IsValid = False
' If (tBoxHome.Text <> "" OrElse tBoxCell.Text <> "" OrElse tBoxWork.Text <> "") Then
' args.IsValid = True
' End If
' End If
'End Sub
如果这是草率的,请告诉我如何/如何解决它,我会。在Visual Basic和StackOverFlow中,我仍然是一个新手,看到这是我的第一篇文章。我希望我错过了一些有助于理解这一点的代码,所以任何问题都要求离开,我会尽快回复。