Link to home
Start Free TrialLog in
Avatar of Type25
Type25

asked on

Urgent - call aspx function from ascx

Very urgent, for some reason i just can't get this working.

I have an aspx page with a function, let's call it  Function1

I also have a user control which sits on the main aspx page, how from the ascx page can i run my function on my aspx page?

Don't want to raise any events.

Just want to do something like

myaspxpage = (myaspxpage)this.parent.page;

myaspxpage.Function1();

But it just isn't happening, won't cast the page like that.

Any ideas?

Thanks
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

just asking .. should myfunction need to be public?
Avatar of Type25
Type25

ASKER

it is public
Avatar of Type25

ASKER

intellisense actually likes the above code and suggests i can call it but it just won't compile
Avatar of Type25

ASKER

Ah ok... now it works..

CompleteReportPhotos pg = (CompleteReportPhotos)this.Parent.Page;
pg.returnItemCategories(-1,-1);

How strange. I think VS.NET got slightly confused between compliations.

Avatar of YZlat
put the function in a separate public module, then it will be accessible from every file of the application
actually, if the UserControl is grabbing data from the Page, I would encourage using interfaces...
1. That way any other page that implements the interface can call the user contro1
or
2. when the page implements the interface, you can call the values from the page.

as in
public class MyPage : Page, IMyPage
{
 
      #region IMyPage implementation
      string IMyPage.CategoryId
      {
            get { return ViewState["CategoryId"] == null ? string.Empty : (string)ViewState["CategoryId"]; }
      }
      #endregion
}

public interface IMyPage
{
      string CategoryId { get; }
}

public class MyUserControl : UserControl
{
      protected Label Category;
      private IMyPage PageInterface
      {
            get { return this.Page; }
      }
      
      private void Page_Load(object sender,EventArgs e)
      {
            if (! this.IsPostBack)
            {
                  this.Category.Text = this.PageInterface.CategoryId;
            }
      }
      
      private void Page_Init(object sender,EventArgs e)
      {
            IMyPage _interface = this.Page as IMyPage;
            if (_interface == null)
                  throw new ApplicationException("Parent Page Must Implement The IMyPage Interface");
      }
      
}

That way, you are controlling from the page what data gets sent to where...a very elegant delivery pathway if i might say so...
Isn't that with just

myaspxpage = (myaspxpage)this.Page;
myaspxpage.Function1();

should be working?
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial