I have a method HasAdminRole() on home.aspx that I call from an ASCX control. And the ASCX works using (Home)this.Page).HasAdminRole() with the user control on home.aspx
Now, when I add that same ASCX to a different ASPX page Adminuser.aspx, I get an error when the ASCX tries to call (Home)this.Page).HasAdminRole() from a different ASPX page than when on home.aspx
I get the error Unable to cast object of type 'ASP.adminuser_aspx' to type 'CALE.Home'.
How do I resolve this when using the same ASCX on multiple ASPX's and the user control tries to call the single method on Home.aspx
The code works when the current page is Home.aspx. But, when I go to a different page via the Menu.asxc the (Home)this.Page fails.
I even tried Home page = Page as Home; and change the code to (Home)page.HasAdminRole() and page is null when on a different aspx page. WHen on Home.aspx, page is NOT null.
ASCX
Menu_load()
{
if ((((Home)this.Page).HasAdminRole()) && (Menu1.Items.Count < 6)) //dont keep building admin when home page redisplayed
in my opinion you should think about to move the HasAdminRole method to a separate class or library and just call this method from the different sites with the appropriate parameters. With this approach you shouldn't have any problems.
Regards.
If my post solves your problem, please mark as answer.
Any reference to ((Home)this.page) will throw a cast error if the current page is anything except Home.aspx.
I also have to execute button click events on home.aspx and these are failing as well.
Unable to cast object of type 'ASP.adminuser_aspx' to type 'CALE.Home'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'ASP.adminuser_aspx' to type 'CALE.Home'.
Source Error:
Line 21: Panel pnl;
Line 22: if (Menu1.SelectedValue == "myprograms")
Line 23: ((Home)this.Page).btnMyPrograms_Click(sender, e);
Line 24: Line 25: if (Menu1.SelectedValue == "showestimateportfolio")
I notice since you've put your ascx control into "Adminuser.aspx". So "Adminuser" must be a class. Of course it's no relationship with Home. You cannot do a convert directly.
My suggestions are (you can try any of them):
1) ((Adminuser)this.Page).HasAdminRole();
2) (this.Page.FindControl("Your Control Id") as YourControlClass).HasAdminRole();
3) if you are not dynamically loading the control, just use YourControlInstanceId.HasAdminRole();
lmmmmm
Member
426 Points
97 Posts
Calling an ASPX method from an ASCX control
Oct 31, 2011 08:26 PM|LINK
I have a method HasAdminRole() on home.aspx that I call from an ASCX control. And the ASCX works using (Home)this.Page).HasAdminRole() with the user control on home.aspx
Now, when I add that same ASCX to a different ASPX page Adminuser.aspx, I get an error when the ASCX tries to call (Home)this.Page).HasAdminRole() from a different ASPX page than when on home.aspx
I get the error Unable to cast object of type 'ASP.adminuser_aspx' to type 'CALE.Home'.
How do I resolve this when using the same ASCX on multiple ASPX's and the user control tries to call the single method on Home.aspx
aspx
Bikram Saluj...
Participant
1582 Points
347 Posts
Re: Calling an ASPX method from an ASCX control
Oct 31, 2011 08:33 PM|LINK
can you post your code as well?
-------------
Please remember to Mark As Answer if this post answered your question!
lmmmmm
Member
426 Points
97 Posts
Re: Calling an ASPX method from an ASCX control
Oct 31, 2011 08:40 PM|LINK
The ASCX is a menu.
The code works when the current page is Home.aspx. But, when I go to a different page via the Menu.asxc the (Home)this.Page fails.
I even tried Home page = Page as Home; and change the code to (Home)page.HasAdminRole() and page is null when on a different aspx page. WHen on Home.aspx, page is NOT null.
ASCX
Menu_load()
{
if ((((Home)this.Page).HasAdminRole()) && (Menu1.Items.Count < 6)) //dont keep building admin when home page redisplayed
blah blah blah
}
ASPX
Home.aspx
public void HasAdminRole()
{
}
Horizon_Net
Star
8277 Points
1435 Posts
Re: Calling an ASPX method from an ASCX control
Oct 31, 2011 08:49 PM|LINK
Hi,
in my opinion you should think about to move the HasAdminRole method to a separate class or library and just call this method from the different sites with the appropriate parameters. With this approach you shouldn't have any problems.
If my post solves your problem, please mark as answer.
lmmmmm
Member
426 Points
97 Posts
Re: Calling an ASPX method from an ASCX control
Oct 31, 2011 09:23 PM|LINK
it is more than just making a separate class.
Any reference to ((Home)this.page) will throw a cast error if the current page is anything except Home.aspx.
I also have to execute button click events on home.aspx and these are failing as well.
Unable to cast object of type 'ASP.adminuser_aspx' to type 'CALE.Home'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'ASP.adminuser_aspx' to type 'CALE.Home'.
Source Error:
Line 21: Panel pnl;
Line 22: if (Menu1.SelectedValue == "myprograms")
Line 23: ((Home)this.Page).btnMyPrograms_Click(sender, e);
Line 24:
Line 25: if (Menu1.SelectedValue == "showestimateportfolio")
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Calling an ASPX method from an ASCX control
Nov 02, 2011 03:46 AM|LINK
Hello lmmmmm :)
I notice since you've put your ascx control into "Adminuser.aspx". So "Adminuser" must be a class. Of course it's no relationship with Home. You cannot do a convert directly.
My suggestions are (you can try any of them):
1) ((Adminuser)this.Page).HasAdminRole();
2) (this.Page.FindControl("Your Control Id") as YourControlClass).HasAdminRole();
3) if you are not dynamically loading the control, just use YourControlInstanceId.HasAdminRole();