c# - Pass variables and access methods of User Control from CodeBehind -


so have function renderwidget() in default.aspx.cs. idea of add user control page.

public void renderwidget(string data) {     control ctrl = page.loadcontrol("/widgets/widget.ascx");     datapanel.controls.add(ctrl); } 

this works fine, lovely jubbly. in other end, @ user control, have 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 try access either of properties in user control. when try add ctrl.test() or ctrl.teststring = "test" default.aspx.cs, error "'system.web.ui.control' not contain definition 'test'". feel there's basic i'm missing here.

any appreciated, thanks!

you have cast correct type:

widgetcontrol widget = (widgetcontrol)ctrl; widget.teststring = "foo"; widget.test(); 

loadcontrol returns control , not actual type of usercontrol.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -