Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1857
  • Last Modified:

Calling the OnServerValidate method from a programmatically created custom validation control

I'm dynamically creating a form using the Placeholder control.

The placeholder conatins a number of TextBoxes, DropDown lists and Validation controls. I can create a RequiredFieldValidator control programmatically but I'm having problem creating a CustomValidator Control.

        valxDOB = New CustomValidator
        valxDOB.ID = arrvalxDOB(intInsuredNumber)
        valxDOB.ControlToValidate = arrdropDOBDD(intInsuredNumber)
        valxDOB.OnServerValidate = "DOBValidator_ServerValidate"
        valxDOB.Text = "Invalid Date"
        valxDOB.CssClass = "highlight"
        valxDOB.Display = ValidatorDisplay.Dynamic
        plhOtherInsured.Controls.Add(valxDOB)

I cant use the OnServerValidate method. This doesnt work:

valxDOB.OnServerValidate = "DOBValidator_ServerValidate"

How do I get the validator control to work?

0
gilkesy
Asked:
gilkesy
  • 6
  • 5
1 Solution
 
raterusCommented:
You must recreate all the controls on a postback when you use dynamic controls.  If you don't, events will not be raised!
0
 
gilkesyAuthor Commented:
Yes, the controls are created in a routine called from the Page_Load event.
0
 
raterusCommented:
Can you show me exactly what you are doing in code?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gilkesyAuthor Commented:
This RequiredFieldValidator works:

        valrReqLastName = New RequiredFieldValidator
        valrReqLastName.ID = arrvalrLastName(intInsuredNumber)
        valrReqLastName.ControlToValidate = arrtxtLastName(intInsuredNumber)
        valrReqLastName.Text = "Last Name Required"
        valrReqLastName.CssClass = "highlight"
        valrReqLastName.Display = ValidatorDisplay.Dynamic
        plhOtherInsured.Controls.Add(valrReqLastName)


This CustomValidator doesnt work:

        valxDOB = New CustomValidator
        valxDOB.ID = arrvalxDOB(intInsuredNumber)
        valxDOB.ControlToValidate = arrdropDOBDD(intInsuredNumber)
        valxDOB.OnServerValidate = "DOBValidator_ServerValidate"
        valxDOB.Text = "Invalid Date"
        valxDOB.CssClass = "highlight"
        valxDOB.Display = ValidatorDisplay.Dynamic
        plhOtherInsured.Controls.Add(valxDOB)


Visual Studio says that " 'System.Web.UI.WebControls.CustomValidator.Protected Overridable Function OnServerValidate(value as String) As Boolean' is not accessible in this context becuase it is 'Protected' "
0
 
raterusCommented:
Can you show me your whole page_load function?  I bet if you set EnableClientScript = False in your RequiredFieldValidator it will stop working too...
0
 
gilkesyAuthor Commented:
   Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
       
        pnlMedical.Visible = False
        If Not IsPostBack Then
                  
            ' Populate the date fields
            ' day first
            Dim i As Integer
            
            dropDOBDD1.Items.Add("DD")
            For i = 1 To 31
                dropDOBDD1.Items.Add(i)
            Next
            
            ' month
            
            colMonth = New SortedList()
            colMonth.Add(0, "MM")
            colMonth.Add(1, "Jan")
            colMonth.Add(2, "Feb")
            colMonth.Add(3, "Mar")
            colMonth.Add(4, "Apr")
            colMonth.Add(5, "May")
            colMonth.Add(6, "Jun")
            colMonth.Add(7, "Jul")
            colMonth.Add(8, "Aug")
            colMonth.Add(9, "Sept")
            colMonth.Add(10, "Oct")
            colMonth.Add(11, "Nov")
            colMonth.Add(12, "Dec")
                  
            dropDOBMM1.DataValueField = "Key"
            dropDOBMM1.DataTextField = "Value"
            dropDOBMM1.DataSource = colMonth
            dropDOBMM1.DataBind()
            
            ' year
            Dim intYear As Integer = Convert.ToString(Year(Now))
            
            i = CInt(intYear)
            dropDOBYY1.Items.Add("YY")
            For i = (i - 65) To i
                dropDOBYY1.Items.Add(i)
            Next
            
        End If
            
        If Session("CountAll") > 1 Then
            Dim litlabel As LiteralControl
            litlabel = New LiteralControl
            litlabel.Text = "<h2 class=""quotehead""> Details of the Other Insured: </h2><table border=""0"" cellpadding=""0"" cellspacing=""0"" width=""100%"">"
            plhOtherInsured.Controls.Add(litlabel)
            For intCounter = 2 To Session("CountAll")
                AddInsuredDetails(intCounter)
            Next
            litlabel = New LiteralControl
            litlabel.Text = "</table>"
            plhOtherInsured.Controls.Add(litlabel)
        End If
            
    End Sub
0
 
gilkesyAuthor Commented:
I thought just changing this:

valxDOB.OnServerValidate = "DOBValidator_ServerValidate"

To this might help:

AddHandler valxDOB.ServerValidate, AddressOf DOBValidator_ServerValidate

But it causes this:

Exception Details: System.Web.HttpException: Control 'CustomValidator1' of type 'CustomValidator' must be placed inside a form tag with runat=server.
0
 
gilkesyAuthor Commented:
I dont understand this because this code is creating a CustomValidation control within the Placeholder "plhOtherInsured"

valxDOB = New CustomValidator
valxDOB.ID = arrvalxDOB(intInsuredNumber)
valxDOB.ControlToValidate = arrdropDOBDD(intInsuredNumber)
AddHandler valxDOB.ServerValidate, AddressOf DOBValidator_ServerValidate
valxDOB.Text = "Invalid Date" valxDOB.CssClass = "highlight"
valxDOB.Display = ValidatorDisplay.Dynamic
plhOtherInsured.Controls.Add(valxDOB)


The placeholder control:

<asp:PlaceHolder ID="plhOtherInsured" runat="server" />

is between the form tags so I dont understand how this error occurs:

System.Web.HttpException: Control 'CustomValidator1' of type 'CustomValidator' must be placed inside a form tag with runat=server.
0
 
raterusCommented:
I don't really know what to tell you, those runat=server errors can sometimes be very cryptic indeed.  What's your general reasoning behind the dynamic controls in the first place.  I keep dynamic controls out of my programs if at all possible (and so far, I don't think I have a one!)
0
 
gilkesyAuthor Commented:
Raterus

If there was an example of the fact that we all should take a break from the computer now and again this is it.

At 1 o'clock this morning I gave up on the problems mentioned above to get some much needed sleep.

When I came back to the computer at 9am (feeling refreshed) i discovered within 5 minutes that somehow I had manually placed a CustomValidator control outside of the form tag.

Once I deleted this control everything worked fine.

Thanks for help though.

Stephen
0
 
raterusCommented:
mmm...sleep, glad you got it worked out!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now