locked
Calling an ASPX method from an ASCX control RRS feed

  • Question

  • User1141085724 posted

    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


    Monday, October 31, 2011 4:26 PM

Answers

  • User3866881 posted

    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();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 1, 2011 11:46 PM

All replies

  • User-608283337 posted

    can you post your code as well?

    Monday, October 31, 2011 4:33 PM
  • User1141085724 posted

    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()

    {

    }

    Monday, October 31, 2011 4:40 PM
  • User455156504 posted

    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.

    Monday, October 31, 2011 4:49 PM
  • User1141085724 posted

    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")
     


    Monday, October 31, 2011 5:23 PM
  • User3866881 posted

    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();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 1, 2011 11:46 PM