473,418 Members | 2,354 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,418 software developers and data experts.

Best way to pass variable between page and control

I'm still trying to fully understand how best to pass variables between
pages/usercontrols/each other.

On a current site I've done, I've had one userControl do the logic and set
the variable, and then I had other usercontrols simply read this by
traversing the class structure: siteClass.userControlClass.specficVariable.

That worked fine.

The new site I'm working on is a bit different, as I'm using multiple
usercontrols on multiple 'subsites' The 'subsite.aspx' page is the page that
will contain the variable.

The structure is like this:

projectClass
- subsite1.aspx
- - uses usercontrol1
- - uses usercontrol2
- subsite2.aspx
- - uses usercontrol1
- - uses usercontrol2

Let's say I have a variable of subsite="1" that I set on the
subsite1.aspx.vb page.

I know what usercontrol1 and usercontrol2 to read that when they load.

Can I simply traverse up the class and grab it on each load? The catch is
that the usercontrol doesn't know what page is loading it at any given time.
I need something like: page.parent.subsite

Or, is there a better way to go about doing this?

-Darrel
Nov 19 '05 #1
8 6555
Check out my tutorial on Page-User control communication:
http://openmymind.net/communication/index.html

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"darrel" <no*****@hotmail.com> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
I'm still trying to fully understand how best to pass variables between
pages/usercontrols/each other.

On a current site I've done, I've had one userControl do the logic and set
the variable, and then I had other usercontrols simply read this by
traversing the class structure: siteClass.userControlClass.specficVariable.
That worked fine.

The new site I'm working on is a bit different, as I'm using multiple
usercontrols on multiple 'subsites' The 'subsite.aspx' page is the page that will contain the variable.

The structure is like this:

projectClass
- subsite1.aspx
- - uses usercontrol1
- - uses usercontrol2
- subsite2.aspx
- - uses usercontrol1
- - uses usercontrol2

Let's say I have a variable of subsite="1" that I set on the
subsite1.aspx.vb page.

I know what usercontrol1 and usercontrol2 to read that when they load.

Can I simply traverse up the class and grab it on each load? The catch is
that the usercontrol doesn't know what page is loading it at any given time. I need something like: page.parent.subsite

Or, is there a better way to go about doing this?

-Darrel

Nov 19 '05 #2
"darrel" <no*****@hotmail.com> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
I'm still trying to fully understand how best to pass variables between
pages/usercontrols/each other.

On a current site I've done, I've had one userControl do the logic and set
the variable, and then I had other usercontrols simply read this by
traversing the class structure:
siteClass.userControlClass.specficVariable.


Your page and your UserControl are both instances of classes. These classes
can have properties. The page can pass data to a UserControl by setting
properties of the UserControl.

John Saunders
Nov 19 '05 #3
Your user controls should expose public properties, methods, and events for
the page to consume. This allows the page and control to pass values
between each other.

Here's a nice, simple way to pass values from one page to another:
(VB.NET code)

'Add data to the context object before transferring
Context.Items("myParameter") = x
Server.Transfer("WebForm2.aspx")

Then, in WebForm2.aspx:

'Grab data from the context property
Dim x as Integer = CType(Context.Items("myParameter"),Integer)

Of course there are a number of ways to pass values from one page to
another, such as using the querystring, cookies, session,
context, saving to a temporary table in the database between each page, etc.
You'll have to decide which technique is best for your application.
Here are several good articles on the subject to help you decide.
http://msdn.microsoft.com/msdnmag/is...e/default.aspx

http://www.aspalliance.com/kenc/passval.aspx

http://www.dotnetjunkies.com/tutoria...tutorialid=600

http://www.dotnetbips.com/displayarticle.aspx?id=79

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com
"darrel" <no*****@hotmail.com> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
I'm still trying to fully understand how best to pass variables between
pages/usercontrols/each other.

On a current site I've done, I've had one userControl do the logic and set
the variable, and then I had other usercontrols simply read this by
traversing the class structure:
siteClass.userControlClass.specficVariable.

That worked fine.

The new site I'm working on is a bit different, as I'm using multiple
usercontrols on multiple 'subsites' The 'subsite.aspx' page is the page
that
will contain the variable.

The structure is like this:

projectClass
- subsite1.aspx
- - uses usercontrol1
- - uses usercontrol2
- subsite2.aspx
- - uses usercontrol1
- - uses usercontrol2

Let's say I have a variable of subsite="1" that I set on the
subsite1.aspx.vb page.

I know what usercontrol1 and usercontrol2 to read that when they load.

Can I simply traverse up the class and grab it on each load? The catch is
that the usercontrol doesn't know what page is loading it at any given
time.
I need something like: page.parent.subsite

Or, is there a better way to go about doing this?

-Darrel

Nov 19 '05 #4
> Your page and your UserControl are both instances of classes. These
classes
can have properties. The page can pass data to a UserControl by setting
properties of the UserControl.


The drawback to that, IMHO, is that I'd need to send that data to each an
every control I load on that page, vs. just letting the controls read it
themselves.

It seems more logical in my head to have the parent dictate the variable and
the children controls read it. Is it better to have the parent set the
variable rather than the children read it?

-Darrel
Nov 19 '05 #5
> Check out my tutorial on Page-User control communication:
http://openmymind.net/communication/index.html


Karl:

nice tutorial! Printint it out now for closer inspection!

It definitely sent me down the right path. This works:

district = CType(Page, parentPageClass).variable

however, don't know which page will be loading the control, so I need to
grab that dynamically. I've tried this:

dim parentPage as string = Parent.ID.ToString

but .net doesn't like the syntax of that.

-Darrel
Nov 19 '05 #6
"darrel" <no*****@hotmail.com> wrote in message
news:OV**************@TK2MSFTNGP09.phx.gbl...
Your page and your UserControl are both instances of classes. These classes
can have properties. The page can pass data to a UserControl by setting
properties of the UserControl.


The drawback to that, IMHO, is that I'd need to send that data to each an
every control I load on that page, vs. just letting the controls read it
themselves.


Only the controls which use it. Do you really have that many controls using
this one variable?

In that case, there are other ways to do this. Session state is one, and
Context.Items is another.

You definitely do not want user controls knowing enough about the structure
of the page to "find" this variable on their own.
It seems more logical in my head to have the parent dictate the variable
and
the children controls read it. Is it better to have the parent set the
variable rather than the children read it?


Yes. This way, the controls are more independant of the details of the page.
If the page decides to do things differently, or use different controls,
then it works. The page is in control of telling the controls what to do,
and the controls can raise events to tell the page what they've done.

John Saunders
Nov 19 '05 #7
> Yes. This way, the controls are more independant of the details of the
page.
If the page decides to do things differently, or use different controls,
then it works. The page is in control of telling the controls what to do,
and the controls can raise events to tell the page what they've done.


John:

Thanks, that's helping me straighten this out in my head.

So, it sounds like parents should always tell children what they need to
know to get the job done, and children should tell the parent if things have
changed that the parent should know about?

A more specific example, this template is telling the child control what
data it should be pulling in:

The parent says to the UC, grab 'group 9'
the UC then gets the content for 'group 9' from the DB and renders it on the
page.
I then have a variety of events that can happen on this UC. One of them, I'd
like the UC to be able to tell the parent page to unload/hide a different UC
that was loaded.

In this case, I'd want the parent, as it loads the UC, to pass a variable or
paramater to tell it to grab 'group 9' data.

Then, I want an event on the UC to then pass something back to the parent
page to tell it to get rid of another UC, if it exists.

Does that sound right? What's the suggested method for passing data back to
the parent page from a UC? Or, should the parent simply be reading this data
from the UC on each pageload/postback?

-Darrel
Nov 19 '05 #8
"darrel" <no*****@hotmail.com> wrote in message
news:uf**************@TK2MSFTNGP14.phx.gbl...
Yes. This way, the controls are more independant of the details of the

page.
If the page decides to do things differently, or use different controls,
then it works. The page is in control of telling the controls what to do,
and the controls can raise events to tell the page what they've done.


John:

Thanks, that's helping me straighten this out in my head.

So, it sounds like parents should always tell children what they need to
know to get the job done, and children should tell the parent if things
have
changed that the parent should know about?

A more specific example, this template is telling the child control what
data it should be pulling in:

The parent says to the UC, grab 'group 9'
the UC then gets the content for 'group 9' from the DB and renders it on
the
page.
I then have a variety of events that can happen on this UC. One of them,
I'd
like the UC to be able to tell the parent page to unload/hide a different
UC
that was loaded.

In this case, I'd want the parent, as it loads the UC, to pass a variable
or
paramater to tell it to grab 'group 9' data.

Then, I want an event on the UC to then pass something back to the parent
page to tell it to get rid of another UC, if it exists.

Does that sound right? What's the suggested method for passing data back
to
the parent page from a UC? Or, should the parent simply be reading this
data
from the UC on each pageload/postback?


This sounds pretty good. I have a few comments.

One, the one UC shouldn't be telling the parent to get rid of the other UC.
The one UC should tell the parent that something has happened. The parent
would then decide that because that something had happened, the other UC
should be removed. To the extent that it's reasonable, one UC should be
independant on the others.

You can pass data back to the parent either in a property (the parent can
read the property), or in the EventArgs-derived object passed in an event
raised by the child. For instance, let's say that your first UC does a
calculation on the group it was told to grab, but finds that the calculation
produced invalid results. The UC can inform the parent of this via an event,
and can pass the bogus value back to the parent in the EventArgs-derived
object.

John Saunders
Nov 19 '05 #9

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

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
8
by: Ian Davies | last post by:
Hello I am trying to run a few INSERT queries only if the user clicks 'continue' in a <a href> The queries takes variables created from SELECT queries. I have tried to acheive this by putting The...
3
by: Hermit Dave | last post by:
Hi, Trying to pass a value to user control within a page. 1. Couldnt find a way to pass it from code behind file. 2. Trying to pass it from aspx page... using EditURL='<% "mypage.aspx?myvar=" +...
9
by: Frank Rizzo | last post by:
I've got a number of user controls on the web page. How can I pass some data to it? I don't see where the user control is instantiated in the page code-behind page. Thanks.
4
by: Josh Harris | last post by:
Here is my question: It is common to have many pieces of business logic encapsulated within asp.net user controls. This can be found in high visibility projects such as the iBuySpy portal from...
12
by: Phil Certain | last post by:
Hi, I'm trying to do something very simple...or at least it should be. I have created a host page (gen.aspx) and a very simple user control (us.ascx). The corresponding code-behind files are...
5
by: GRB | last post by:
I'm using VB.Net 2003 to build a web page. I need to prompt the client for a name in order to save a list. I used the following code to btnSaveList.Attributes.Add("onclick", "var listname =...
4
by: simon | last post by:
hello. relatively new to vb.net, i'm using VS 2003 and .net 2.0 i have a web app that i'm i have a user control that displays a simple 1 row table as the header of the page. the user control...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.