how to declare a user control in code behind

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

How do I declare in the parent page's code behind a user control? I want to
call a sub that is located in the user control.

Want to do something like this:
Call MyUserControl.MySub()
 
Your User Control is (probably) going to be a member of the WebForm's
Controls Collection, and you can use the FindControl() method to locate it
and call any Methods you want. Example:

Page.FindControl("Form1").FindControl("MyUserControlID").MySub()

The FindControl() method of a Server Control will look in the immediate
children of the Control, and return a reference to the Control found. That
is why I used the FindControl() method on the Page to get the Form, and the
FindControl() Method of the Form to find the UserControl. If the User
Control were inside another Control, you would look in that Contro's
Controls Collection for it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Along the same lines....
How would you get the text from a form field - textbox, actually - in the
User Control to use in the parent page?

Thanks,
Bob Lehmann
 
No, that's not it. It's very simple: call a method in a user control from
the parent page.

I can do this on the ASPX page:
dim ucPager1 as New ucPager 'ucPager is the user control
Call ucPager1.mySub() 'mySub is a sub I wrote in the user control

This allows me to call mySub, but then mySub cannot use any controls on the
page -- undoubtably, because it is a New instance.

So how would I do this without creating a new instance?
 
No, that's not it. It's very simple: call a method in a user control from

I'm afraid that IS it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Thanks for your help but I get an error:

Page.FindControl("Form1").FindControl("WucPager1").SetPaging()
Gives me syntax error: 'SetPaging' is not a member of
'System.Web.UI.Control'.

SetPaging is not a control, it's a Public method I wrote in the user
control. It can't be a shared method either.

I call this function in the page_load event, so perhaps the user control is
not initialized before then... although it would have to be...

Any ideas?
 
Wow, I added some casting and everything in my application is working now!!

Call CType(Page.FindControl("Form1").FindControl("WucPager1"),
wucPager).SetPaging()

wucPager is the user control

This is very strange to me that it takes a long reference to do what
essentially is this:
wucPager.SetPaging() - but this only works if SetPaging is public shared.

-Max
 
You have to identify the Control that is your User Control's immediate
Parent, and call the FindControl() method on that Control to find it. If
you're not sure, you can wirte a recursive function that goes through every
child Control of a given Control and calls itself to iterate through each
Control in the Page.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top