473,394 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to declare a user control in code behind

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()
Nov 18 '05 #1
7 5298
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("MyUserContr olID").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.

"Max" <ma*****@portvista.com> wrote in message
news:cF*****************@twister.tampabay.rr.com.. .
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()

Nov 18 '05 #2
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

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:eH**************@TK2MSFTNGP12.phx.gbl...
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("MyUserContr olID").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.

"Max" <ma*****@portvista.com> wrote in message
news:cF*****************@twister.tampabay.rr.com.. .
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()


Nov 18 '05 #3
Max
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?

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:eH**************@TK2MSFTNGP12.phx.gbl...
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("MyUserContr olID").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.

Nov 18 '05 #4
> 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.

"Max" <ma*****@portvista.com> wrote in message
news:MG******************@twister.tampabay.rr.com. ..
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?

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:eH**************@TK2MSFTNGP12.phx.gbl...
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("MyUserContr olID").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.


Nov 18 '05 #5
Max
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?


"Kevin Spencer" <ke***@takempis.com> wrote
No, that's not it. It's very simple: call a method in a user control from

I'm afraid that IS it.
Page.FindControl("Form1").FindControl("MyUserContr olID").MySub()


Nov 18 '05 #6
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.

"Max" <ma*****@portvista.com> wrote in message
news:Gp******************@twister.tampabay.rr.com. ..
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?


"Kevin Spencer" <ke***@takempis.com> wrote
No, that's not it. It's very simple: call a method in a user control

from

I'm afraid that IS it.
> Page.FindControl("Form1").FindControl("MyUserContr olID").MySub()
>


Nov 18 '05 #7
Max
Wow, I added some casting and everything in my application is working now!!

Call CType(Page.FindControl("Form1").FindControl("WucPa ger1"),
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
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
No, that's not it. It's very simple: call a method in a user control
from
I'm afraid that IS it.

Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
2
by: Technical Support | last post by:
Good afternoon, I created a custom control, compiled it into a DLL, and added it to the BIN directory. I placed an instance of the control in a webForm and am trying to programatically change its...
1
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
2
by: Brandon Potter | last post by:
Why is it that when I override the OnInit method for a page, I can add a user control like this: Me.Controls.Add(LoadControl("MyControl.ascx")) (where MyControl.ascx is your every day user...
1
by: TJS | last post by:
this is the code in the html <tabs:menu id="Menu1" runat="server"></tabs:menu> how do I declare "Menu1" in the codeBehind ??
2
by: N. Demos | last post by:
I have a user control with code behind of which two instances are created/declared in my aspx page. The aspx page has code behind also, as I need to access methods of the usercontrols on page...
2
by: Alan Silver | last post by:
Hello, I have a user control that has one .cs file, with several .ascx files that call it. The reason for this is that I can pick whichever of the ..ascx files I want (based on a site owner...
9
by: Alan Silver | last post by:
Hello, I have a user control which I am trying to load dynamically, but am running into problems. I think the problem is because I have two .ascx files that refer to the same .ascx.cs file. A...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.