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,
FernandoChange this line of code:
Page callingPage = (Page) this.Page;
to:
MyClass callingPage = (MyClass) this.Page;
Fernando Chilvarguer wrote:
Quote:
Originally Posted by
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
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" <n_o_s_p_a__m@.mail.comwrote in message
news:1157043962.662681.143570@.m73g2000cwd.googlegr oups.com...
Quote:
Originally Posted by
Change this line of code:
>
Page callingPage = (Page) this.Page;
>
to:
>
MyClass callingPage = (MyClass) this.Page;
>
Fernando Chilvarguer wrote:
Quote:
Originally Posted by
>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
>
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:
Quote:
Originally Posted by
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" <n_o_s_p_a__m@.mail.comwrote in message
news:1157043962.662681.143570@.m73g2000cwd.googlegr oups.com...
Quote:
Originally Posted by
Change this line of code:
Page callingPage = (Page) this.Page;
to:
MyClass callingPage = (MyClass) this.Page;
Fernando Chilvarguer wrote:
Quote:
Originally Posted by
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
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" <fernando@.no-spam-impex.comwrote in message
news:uQ%23o97RzGHA.2036@.TK2MSFTNGP05.phx.gbl...
Quote:
Originally Posted by
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
>
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" <n_o_s_p_a__m@.mail.comwrote in message
news:1157061762.574176.201880@.h48g2000cwc.googlegr oups.com...
Quote:
Originally Posted by
>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:
Quote:
Originally Posted by
>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" <n_o_s_p_a__m@.mail.comwrote in message
>news:1157043962.662681.143570@.m73g2000cwd.googlegr oups.com...
Quote:
Originally Posted by
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
>
>
0 comments:
Post a Comment