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");
dataPanel.Controls.Add(ctrl);
}
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() {
Response.Write("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!
您需要将实例转换为WidgetControl
。
You have to cast to the correct type:
WidgetControl widget = (WidgetControl)ctrl;
widget.testString = "Foo";
widget.test();
LoadControl
returns Control
and not the actual type of your UserControl
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.