473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing variables from ASPX page to ASCX control

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
Aug 31 '06 #1
5 14323
KJ
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
Aug 31 '06 #2
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_**********@mail.comwrote in message
news:11**********************@m73g2000cwd.googlegr 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

Aug 31 '06 #3
KJ
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" <n_**********@mail.comwrote in message
news:11**********************@m73g2000cwd.googlegr 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
Aug 31 '06 #4
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" <fe******@no-spam-impex.comwrote in message
news:uQ****************@TK2MSFTNGP05.phx.gbl...
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

Aug 31 '06 #5
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_**********@mail.comwrote in message
news:11**********************@h48g2000cwc.googlegr 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" <n_**********@mail.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.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

Sep 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Paul Yanzick | last post by:
Hello, I am trying to develop a book tracking application for my capstone in school, and am running into a problem. The application is an ASP.Net application written in C#. The first page you...
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
5
by: DotNet | last post by:
I'm trying to construct a header user control and would like to pass an integer to the control in order to display the correct on state for a menu. How does one pass an integer from an .aspx page...
2
by: darrel | last post by:
Is there a SOP for setting variables on an ASPX page that the usercontrols on that page can read? I'd like to set a 'template=' variable that various other controls can see (the header,...
4
by: David Freeman | last post by:
Hi There! I'm just wondering if there's a way to pass parameters (as if you were passing parameters to a ASCX web control) when calling an ASPX page? e.g. MyDetailsPage.UserName = "david" ...
2
by: Ric | last post by:
im new to asp.net. from what i understand, you have the aspx file (presentation), user-control(ascx file), code-behind(vb file) and components(compiled vb and dll files). the aspx file contains a...
3
by: voro.cibus | last post by:
I have been reading up on this all day, and I can't find the answer (or more likely, don't understand the answers I have found) to my problem. I have a table that stores the name of my ascx page....
1
by: billy | last post by:
Ok, here's the situation... I have a user control that contains two textboxes (one for a from date/time and one for a to date/time) and two image buttons. The user control itself is supposed to...
5
by: Ludwig | last post by:
I have a user control news.ascx that lists news items, and it has add/edit/delete linkbuttons. This user control is on the default.aspx page together with other user controls. When the edit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.