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.pa ge;
myaspxpage.Function1();
But it just isn't happening, won't cast the page like that.
Any ideas?
Thanks
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.pa
myaspxpage.Function1();
But it just isn't happening, won't cast the page like that.
Any ideas?
Thanks
just asking .. should myfunction need to be public?
ASKER
it is public
ASKER
intellisense actually likes the above code and suggests i can call it but it just won't compile
ASKER
Ah ok... now it works..
CompleteReportPhotos pg = (CompleteReportPhotos)this .Parent.Pa ge;
pg.returnItemCategories(-1 ,-1);
How strange. I think VS.NET got slightly confused between compliations.
CompleteReportPhotos pg = (CompleteReportPhotos)this
pg.returnItemCategories(-1
How strange. I think VS.NET got slightly confused between compliations.
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["Categor yId"]; }
}
#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.Categor yId;
}
}
private void Page_Init(object sender,EventArgs e)
{
IMyPage _interface = this.Page as IMyPage;
if (_interface == null)
throw new ApplicationException("Pare nt 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...
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["Categor
}
#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.Categor
}
}
private void Page_Init(object sender,EventArgs e)
{
IMyPage _interface = this.Page as IMyPage;
if (_interface == null)
throw new ApplicationException("Pare
}
}
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?
myaspxpage = (myaspxpage)this.Page;
myaspxpage.Function1();
should be working?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.