1

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.

1
  • @gnomixa: How are you making the call? Is it to a type of object? If you are creating dynamically, that screams reflection to me, in which case you have to make the call to the method through reflection as well, unless the method is on a known interface (base class or interface). Commented Mar 2, 2009 at 18:29

5 Answers 5

5

You've said

Control fracTemplateCtrl = 
   (FracTemplateCtrl)LoadControl("FracTemplateCtrl.ascx")
fracTemplateCtrl.TestMethod();

you need to say

FracTemplateCtrl fracTemplateCtrl =
   (FracTemplateCtrl)LoadControl("FracTemplateCtrl.ascx")
fracTemplateCtrl.TestMethod();

Note that fracTemplateCtrl is declared as a FracTemplateCtrl, so visual studio knows that it has a TestMethod(). When it is declared as a Control, visual studio can't make this assumption.

Sign up to request clarification or add additional context in comments.

1 Comment

@gnomixa: No problem. Note that you would have gotten your answer much more quickly had you posted your code with the question! ;]
1

Are you casting the User Control to the correct type?

Comments

1

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:

PersonControl ctl = (PersonControl)LoadControl("PersonControl.ascx");
ctl.DoCustomMethod();

2 Comments

I tried casting, but that didn't work either: Control fracTemplateCtrl = (FracTemplateCtrl)LoadControl("FracTemplateCtrl.ascx"); Form.Controls.Add(fracTemplateCtrl); Response.Write(fracTemplateCtrl.TestMethod()); error is that TestMethod is not found
Your fracTemplateCtrl also needs to be of type FracTemplateCtrl, in addition to the cast. The code in this comment is trying to call TestMethod on a variable of type Control.
1

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.

2 Comments

heh, 14 years later and this worked for me
@Roto that is absolutely crazy that this is still relevant after all this time!
0

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:

public MyControl : UserControl
{
    public void MyMethod(){...}
}

in your form:

private MyControl mycontrol = new MyControl();
private void MainForm()
{
    this.Controls.Add(mycontrol);

    InitializeComponent();
}

private void DoStuff()
{
    ((MyControl)mycontrol).MyMethod();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.