Pass variables and access methods of User Control from CodeBehind

So I have a function renderWidget() in default.aspx.cs. The idea of this is to add a User Control to a page.

public void renderWidget(string data) {
    Control ctrl = Page.LoadControl("/widgets/widget.ascx");

This all works fine, lovely jubbly. Now in the other end, at the User Control, I have the following code in widget.ascx.cs:

public class WidgetControl : System.Web.UI.UserControl
    public string testString = "";
    public void test() {


The problem arises when I try to access either of the properties in the User Control. When I try to add ctrl.test() or ctrl.testString = "test" to default.aspx.cs, I get the error "'System.Web.UI.Control' does not contain a definition for 'test'". I feel like there's probably something very basic I'm missing here.

Any help appreciated, thanks!


You have to cast to the correct type:

WidgetControl widget = (WidgetControl)ctrl;
widget.testString = "Foo";

LoadControl returns Control and not the actual type of your UserControl.

You need to cast your instance to WidgetControl.

Need Your Help

Convert Coin3D SoOffscreenRenderer to QImage and render with OpenGL

c++ qt opengl openinventor

I'm trying to display a Coin3D/Open Inventor scene with QT in a QGLWidget, by using the SoOffscreenRenderer and I need help converting it to a QImage

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.