Last post Oct 31, 2011 05:23 PM by Decker Dong - MSFT
Oct 31, 2011 04:26 PM|lmmmmm|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
Oct 31, 2011 04:33 PM|Bikram Saluja|LINK
can you post your code as well?
Oct 31, 2011 04:40 PM|lmmmmm|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.
if ((((Home)this.Page).HasAdminRole()) && (Menu1.Items.Count < 6)) //dont keep building admin when home page redisplayed
blah blah blah
public void HasAdminRole()
Oct 31, 2011 04:49 PM|Horizon_Net|LINK
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.
Oct 31, 2011 05:23 PM|lmmmmm|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'.
Line 21: Panel pnl;
Line 22: if (Menu1.SelectedValue == "myprograms")
Line 23: ((Home)this.Page).btnMyPrograms_Click(sender, e);
Line 25: if (Menu1.SelectedValue == "showestimateportfolio")
Nov 01, 2011 11:46 PM|Decker Dong - MSFT|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):
2) (this.Page.FindControl("Your Control Id") as YourControlClass).HasAdminRole();
3) if you are not dynamically loading the control, just use YourControlInstanceId.HasAdminRole();