Help with User Control

HLRosenberger
HLRosenberger used Ask the Experts™
on
I'm creating a User Control.   I have attached the ascx and ascx.vb.  I have also attached the aspx and aspx.vb for the page where I am using the control.   Also attached an error message I get when I run.  Question - why the error, why can it not find my txtFirstName control?
ascx.png
ascx.vb.png
aspx.png
aspx.vb.png
error.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Its because the textbox is in a table and will be repeated for each row so it is not unique. For controls that inside repeating controls like gridview items, repeater, tables etc, you can not reference these directly.

Author

Commented:
ah, got it!

Author

Commented:
I removed the table.   That did not help.,  I also added ClientIDMode="Static" to the txtFirstName control.  Still no luck,.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Most Valuable Expert 2012
Top Expert 2014
Commented:
I think the required field validator will try to find the control within the subtree of its parent control and would not be able to find it in the current configuration that you have so this idea may not work.

Author

Commented:
There must be a way.

Author

Commented:
You were correct.  I change the code to dynamically add the controls to the Page.Form

  Private rnd As New Random(DateTime.Now.Millisecond)

Public Sub AddControl(ControlToValidate As String, ErrorMessage As String)

        Dim req As New RequiredFieldValidator
        Dim ext As New ValidatorCalloutExtender

        req.ControlToValidate = ControlToValidate
        req.ErrorMessage = ErrorMessage
        req.Display = ValidatorDisplay.None
        req.ID = "req" & Rnd.Next

        ext.TargetControlID = req.ID
        ext.ID = "ext" & rnd.Next

        Page.Form.Controls.Add(req)
        Page.Form.Controls.Add(ext)

    End Sub

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial