By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Best way to pass variable between page and control

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
> 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

P: n/a
> 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

P: n/a
"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

P: n/a
> 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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.