Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Passing variables from ASPX page to ASCX control

Reply
Thread Tools

Passing variables from ASPX page to ASCX control

 
 
Fernando Chilvarguer
Guest
Posts: n/a
 
      08-31-2006
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


 
Reply With Quote
 
 
 
 
KJ
Guest
Posts: n/a
 
      08-31-2006
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


 
Reply With Quote
 
 
 
 
Fernando Chilvarguer
Guest
Posts: n/a
 
      08-31-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

>



 
Reply With Quote
 
KJ
Guest
Posts: n/a
 
      08-31-2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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

> >


 
Reply With Quote
 
bruce barker \(sqlwork.com\)
Guest
Posts: n/a
 
      08-31-2006
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" <(E-Mail Removed)> wrote in message
news:uQ%(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Fernando Chilvarguer
Guest
Posts: n/a
 
      09-01-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>> > 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
>> >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variables from .aspx.cs code pages to .aspx pages RadioSignal ASP .Net Web Controls 7 10-21-2005 11:41 PM
How to direct <Enter> key event on ascx control within aspx page? KatB ASP .Net 0 06-07-2005 07:43 PM
Is Passing Parameters to an ASPX page like ASCX controls possible? David Freeman ASP .Net Web Controls 4 11-09-2004 05:51 AM
Is Passing Parameters to an ASPX page like ASCX controls possible? David Freeman ASP .Net 4 11-09-2004 05:51 AM
passing variables from aspx page to ascx control darrel ASP .Net 2 05-21-2004 03:55 PM



Advertisments