Calling Usercontrol function

B

Bert

Hi

How can you call a function on a user control(.ascx) ? what is the syntax
for this?

thanks

Bert
 
M

Masudur

Bert said:
Hi

How can you call a function on a user control(.ascx) ? what is the syntax
for this?

thanks

Bert

Hi...

Write function in your user control with public access specifier. Then
from page call the function from the instance of the user control...

Suppose you have a user control Search.ascx and in the user control you
have a function

public void DoSomeWork()
{
///Perform some operation
///This is a usercontrol function
}

And in the aspx page you can access...

protected void Page_Load(object sender, EventArgs e)
{
Search1.DoSomeWork();
}

just like that...

Thanks

Masudur
Kaz Software Ltd.
www.kaz.com.bd
 
B

Bert

forgot this:
my usercontrol is loaded into a placeholder, so how do you call it then from
this aspx page:

Dim uc as Control
uc = me.loadcontrol("myusercontrol.ascx")
me.MyPlaceholder.add(uc)

Say myusercontrol.ascx had a sub dosomething(), how do you call this
dosomething sub from the page?

thanks
 
M

Mark Fitzpatrick

You need to strongly type the user control when you load it. I'm going to do
it in C# as I haven't used VB in a few years.

MySite.MyControl uc =
(MySite.MyControl)me.loadcontrol("myusercontrol.ascx");
uc.MyFunction();

You could also use reflection to do it, but reflection can be a lot slower.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top