How can invoke user control's public method from within the page?
I load the control dynamically inside OnInit on the page.Any ideas? For some reason I am getting a build error that says that the method doesn't exist, even though it's public. Starting to think that user controls are not worth all the hassle.
Without sample code it's hard to say for sure, but I'm guessing you need to cast your reference to the UserControl to the specific type of your custom control. For example, if your UserControl is of type "PersonControl", then your code would look something like this:
If your class is inheriting from UserControl, be sure that when you're accessing it, like others have said, you're casting it correctly. For instance:
in your form:
Are you casting the User Control to the correct type?
Mike's answer is how it's done, but Visual Studio can be a bit wonky about detecting the UserControl class, and complain that the type doesn't exist. In those situations I've have to add a
<% @Reference %>
tag on the page to force it to recognize, even though it may be referenced in the web.config.You've said
you need to say
Note that
fracTemplateCtrl is
declared as aFracTemplateCtrl
, so visual studio knows that it has aTestMethod()
. When it is declared as aControl
, visual studio can't make this assumption.