Calling Usercontrol function

  • Thread starter Thread starter Bert
  • Start date Start date
B

Bert

Hi

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

thanks

Bert
 
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
 
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
 
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.
 
Back
Top