Passing variables from ASPX page to ASCX control

Discussion in 'ASP .Net' started by Fernando Chilvarguer, Aug 31, 2006.

  1. I'm sure this has come up before but I could not find any post on it.

    How can I read a variable or property that has been set on a ASPX page from
    inside a ASCX control.

    ASPX code:

    public partial class MyClass: System.Web.UI.Page
    {
    protected string _companyID;
    public string CompanyID
    {
    get
    {
    return _companyID;
    }
    set
    {
    _companyID = value;
    }
    }
    .....etc...

    ASCX code:

    public partial class Controls_MyControl: System.Web.UI.UserControl,
    IPostBackDataHandler
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Page callingPage = (Page) this.Page;
    //How to read the "CompanyID" property from the calling page??
    }

    Thanks,
    Fernando
     
    Fernando Chilvarguer, Aug 31, 2006
    #1
    1. Advertising

  2. Fernando Chilvarguer

    KJ Guest

    Change this line of code:

    Page callingPage = (Page) this.Page;

    to:

    MyClass callingPage = (MyClass) this.Page;

    Fernando Chilvarguer wrote:
    > I'm sure this has come up before but I could not find any post on it.
    >
    > How can I read a variable or property that has been set on a ASPX page from
    > inside a ASCX control.
    >
    > ASPX code:
    >
    > public partial class MyClass: System.Web.UI.Page
    > {
    > protected string _companyID;
    > public string CompanyID
    > {
    > get
    > {
    > return _companyID;
    > }
    > set
    > {
    > _companyID = value;
    > }
    > }
    > ....etc...
    >
    > ASCX code:
    >
    > public partial class Controls_MyControl: System.Web.UI.UserControl,
    > IPostBackDataHandler
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Page callingPage = (Page) this.Page;
    > //How to read the "CompanyID" property from the calling page??
    > }
    >
    > Thanks,
    > Fernando
     
    KJ, Aug 31, 2006
    #2
    1. Advertising

  3. Thanks KJ.

    But when I change to MyClass my code does not compile.
    How to I add a reference to that class in my Control?

    Thanks,
    Fernando
    "KJ" <> wrote in message
    news:...
    > Change this line of code:
    >
    > Page callingPage = (Page) this.Page;
    >
    > to:
    >
    > MyClass callingPage = (MyClass) this.Page;
    >
    > Fernando Chilvarguer wrote:
    >> I'm sure this has come up before but I could not find any post on it.
    >>
    >> How can I read a variable or property that has been set on a ASPX page
    >> from
    >> inside a ASCX control.
    >>
    >> ASPX code:
    >>
    >> public partial class MyClass: System.Web.UI.Page
    >> {
    >> protected string _companyID;
    >> public string CompanyID
    >> {
    >> get
    >> {
    >> return _companyID;
    >> }
    >> set
    >> {
    >> _companyID = value;
    >> }
    >> }
    >> ....etc...
    >>
    >> ASCX code:
    >>
    >> public partial class Controls_MyControl: System.Web.UI.UserControl,
    >> IPostBackDataHandler
    >> {
    >> protected void Page_Load(object sender, EventArgs e)
    >> {
    >> Page callingPage = (Page) this.Page;
    >> //How to read the "CompanyID" property from the calling page??
    >> }
    >>
    >> Thanks,
    >> Fernando

    >
     
    Fernando Chilvarguer, Aug 31, 2006
    #3
  4. Fernando Chilvarguer

    KJ Guest

    I should have recognized that you are using .net 2.0 (this would
    compile in 1.x).

    In this case, I do not know of a means for referencing the parent page
    because doing so (using the @Reference directive, for example) would
    create a circular reference.

    I think a better design approach would be for the parent page to *tell*
    the user control what its CustomerID is.

    That is to say, create a method or property, such as SetCustomerId() or
    CustomerID, on the control, rather than having the control attempt to
    pull the value from the hosting page.

    This may seem like double work, but, it is actually better design
    because you can now put your control on *any* page without requiring
    the hosting page to declare certain properties. The control is supposed
    to be a self-contained entity (black box).

    For example:

    public partial class MyClass: System.Web.UI.Page
    {
    protected string _companyID;
    public string CompanyID
    {
    get
    {
    return _companyID;
    }
    set
    {
    _companyID = value;
    myControl.CompanyID = value; //keeps the control and page in sync
    }
    }


    ASCX code:

    public partial class Controls_MyControl: System.Web.UI.UserControl,
    IPostBackDataHandler
    {
    protected string _companyID;
    public string CompanyID
    {
    get
    {
    return _companyID;
    }
    set
    {
    _companyID = value;
    }
    }
    }

    Fernando Chilvarguer wrote:
    > Thanks KJ.
    >
    > But when I change to MyClass my code does not compile.
    > How to I add a reference to that class in my Control?
    >
    > Thanks,
    > Fernando
    > "KJ" <> wrote in message
    > news:...
    > > Change this line of code:
    > >
    > > Page callingPage = (Page) this.Page;
    > >
    > > to:
    > >
    > > MyClass callingPage = (MyClass) this.Page;
    > >
    > > Fernando Chilvarguer wrote:
    > >> I'm sure this has come up before but I could not find any post on it.
    > >>
    > >> How can I read a variable or property that has been set on a ASPX page
    > >> from
    > >> inside a ASCX control.
    > >>
    > >> ASPX code:
    > >>
    > >> public partial class MyClass: System.Web.UI.Page
    > >> {
    > >> protected string _companyID;
    > >> public string CompanyID
    > >> {
    > >> get
    > >> {
    > >> return _companyID;
    > >> }
    > >> set
    > >> {
    > >> _companyID = value;
    > >> }
    > >> }
    > >> ....etc...
    > >>
    > >> ASCX code:
    > >>
    > >> public partial class Controls_MyControl: System.Web.UI.UserControl,
    > >> IPostBackDataHandler
    > >> {
    > >> protected void Page_Load(object sender, EventArgs e)
    > >> {
    > >> Page callingPage = (Page) this.Page;
    > >> //How to read the "CompanyID" property from the calling page??
    > >> }
    > >>
    > >> Thanks,
    > >> Fernando

    > >
     
    KJ, Aug 31, 2006
    #4
  5. define an interface in app_code that returns companyid. the have you pages
    implement the interface (could inherit from a base page that implments the
    interface. cast the page to the interface, and call method.

    -- bruce (sqlwork.com)


    "Fernando Chilvarguer" <> wrote in message
    news:uQ%...
    > I'm sure this has come up before but I could not find any post on it.
    >
    > How can I read a variable or property that has been set on a ASPX page
    > from inside a ASCX control.
    >
    > ASPX code:
    >
    > public partial class MyClass: System.Web.UI.Page
    > {
    > protected string _companyID;
    > public string CompanyID
    > {
    > get
    > {
    > return _companyID;
    > }
    > set
    > {
    > _companyID = value;
    > }
    > }
    > ....etc...
    >
    > ASCX code:
    >
    > public partial class Controls_MyControl: System.Web.UI.UserControl,
    > IPostBackDataHandler
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Page callingPage = (Page) this.Page;
    > //How to read the "CompanyID" property from the calling page??
    > }
    >
    > Thanks,
    > Fernando
    >
     
    bruce barker \(sqlwork.com\), Sep 1, 2006
    #5
  6. Thanks again KJ.

    I used the suggested aproach (black-box) and it's all working great!
    I set all the control properties from the aspx page. I also implemented all
    the error-handling in case the control gets dropped into a page that does
    not set up its properties.


    THANKS!

    "KJ" <> wrote in message
    news:...
    >I should have recognized that you are using .net 2.0 (this would
    > compile in 1.x).
    >
    > In this case, I do not know of a means for referencing the parent page
    > because doing so (using the @Reference directive, for example) would
    > create a circular reference.
    >
    > I think a better design approach would be for the parent page to *tell*
    > the user control what its CustomerID is.
    >
    > That is to say, create a method or property, such as SetCustomerId() or
    > CustomerID, on the control, rather than having the control attempt to
    > pull the value from the hosting page.
    >
    > This may seem like double work, but, it is actually better design
    > because you can now put your control on *any* page without requiring
    > the hosting page to declare certain properties. The control is supposed
    > to be a self-contained entity (black box).
    >
    > For example:
    >
    > public partial class MyClass: System.Web.UI.Page
    > {
    > protected string _companyID;
    > public string CompanyID
    > {
    > get
    > {
    > return _companyID;
    > }
    > set
    > {
    > _companyID = value;
    > myControl.CompanyID = value; //keeps the control and page in sync
    > }
    > }
    >
    >
    > ASCX code:
    >
    > public partial class Controls_MyControl: System.Web.UI.UserControl,
    > IPostBackDataHandler
    > {
    > protected string _companyID;
    > public string CompanyID
    > {
    > get
    > {
    > return _companyID;
    > }
    > set
    > {
    > _companyID = value;
    > }
    > }
    > }
    >
    > Fernando Chilvarguer wrote:
    >> Thanks KJ.
    >>
    >> But when I change to MyClass my code does not compile.
    >> How to I add a reference to that class in my Control?
    >>
    >> Thanks,
    >> Fernando
    >> "KJ" <> wrote in message
    >> news:...
    >> > Change this line of code:
    >> >
    >> > Page callingPage = (Page) this.Page;
    >> >
    >> > to:
    >> >
    >> > MyClass callingPage = (MyClass) this.Page;
    >> >
    >> > Fernando Chilvarguer wrote:
    >> >> I'm sure this has come up before but I could not find any post on it.
    >> >>
    >> >> How can I read a variable or property that has been set on a ASPX page
    >> >> from
    >> >> inside a ASCX control.
    >> >>
    >> >> ASPX code:
    >> >>
    >> >> public partial class MyClass: System.Web.UI.Page
    >> >> {
    >> >> protected string _companyID;
    >> >> public string CompanyID
    >> >> {
    >> >> get
    >> >> {
    >> >> return _companyID;
    >> >> }
    >> >> set
    >> >> {
    >> >> _companyID = value;
    >> >> }
    >> >> }
    >> >> ....etc...
    >> >>
    >> >> ASCX code:
    >> >>
    >> >> public partial class Controls_MyControl: System.Web.UI.UserControl,
    >> >> IPostBackDataHandler
    >> >> {
    >> >> protected void Page_Load(object sender, EventArgs e)
    >> >> {
    >> >> Page callingPage = (Page) this.Page;
    >> >> //How to read the "CompanyID" property from the calling page??
    >> >> }
    >> >>
    >> >> Thanks,
    >> >> Fernando
    >> >

    >
     
    Fernando Chilvarguer, Sep 1, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel
    Replies:
    2
    Views:
    713
    darrel
    May 21, 2004
  2. David Freeman
    Replies:
    4
    Views:
    3,413
    David Freeman
    Nov 9, 2004
  3. KatB
    Replies:
    0
    Views:
    595
  4. David Freeman
    Replies:
    4
    Views:
    212
    David Freeman
    Nov 9, 2004
  5. RadioSignal

    passing variables from .aspx.cs code pages to .aspx pages

    RadioSignal, Sep 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    2,301
    Mirronelli
    Oct 22, 2005
Loading...

Share This Page