Forum Moderators: open

Message Too Old, No Replies

UserControl Woes

Can't find my usercontrol to access it at runtime

         

qlipoth

9:10 pm on Jun 1, 2005 (gmt 0)

10+ Year Member



I have a usercontrol which needs to take initilization info from the page it rests upon.

The control was placed on the page through the nice little drag and drop feature in .net, but when I try to access the controls' initilization method from the page's codebehind, it's never heard of the control.

I tried creating the control {project.control name = new control()}
and then adding it to the form manually {this.controls.add(name)} but that caused all kinds of errors.

Anyone have any ideas?

Jimmy Turnip

8:40 am on Jun 2, 2005 (gmt 0)

10+ Year Member



You need to dim the control in the page code-behind to access any methods or properties of it.

Not sure if you're using c# or vb, but if you look in the "Web Form Designer Generator Code" you'll see all the other web controls on the page defined. Add the usercontrol similarly:

Protected WithEvents MyControl1 As MyControl

Where:

MyControl1 = name of control on the page
MyControl = User control class name

qlipoth

1:38 pm on Jun 2, 2005 (gmt 0)

10+ Year Member



Wow... amazing what a little thing like declaring it in the right place will do.

I was trying to do it in the page_load, and getting two versions of the object, one which was getting the init data, the other displayed on the page.

Thanks muchly for the help.