473,396 Members | 2,010 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.

Reading UC variable from page. (Again... ;o)

Karl has helped me in the past in regards to communicating between controls
and pages:

http://www.openmymind.net/communication/index.html#3.1

I ended up going down the interfaces path and actually got that working
fairly well, but, then, in the end, decided that was overkill and that it
would perhaps make more sense to start over using a much more simpler
communication between pages.

So, this is what I have:

getVariables.ascx
otherControl.ascx
page.aspx

getVariables reads an xml file and sets a variety of strings. We'll use this
generic one:

public pagePropPageTitle as string
'logic to read xml and set the value

on page.aspx, I want to get the pagePropPageTitle value and then send that
onto otherControl.ascx

Is there a way to grab that value directly? I can't just go
getVariables.pagePropPageTitle because that isn't declared. Is there another
way to go about this?

Looking at the above tutorial, it talks about how to do this using
dynamically loaded controls. Is that the only way? If so, is there anything
wrong with dynamically loading all of your controls via the codebehind?

-Darrel
Nov 19 '05 #1
4 1271
Darrel,
Don't mean to keep jumping in and potentially causing other points of views
to be skipped. Additionally, I can't say I fully remember the context of
our conversation. having said that, to specifically answer your question,
there's nothing wrong with loading your controls (even all of them)
dynamically. You'll incur a performance hit, but more than likely something
you won't notice...AND it'll often be the only way you can do something.

Having said that, perhaps we discussed this previously, but it seems to me
that maybe getVariables.ascx shouldn't be a user control. Does it display
anything or does it simply get variables from XML and make them available?
If it doesn't have a presentation component (and from your description it
doesn't), you really should be making us of a class, perhaps:
VariableManager.vb (or .cs depending on what poison you prefer). Even if
getVariables.ascx DOES have a presentation element, the part which interacts
with the XML file more than likely belongs in a class, in your business
layer, not your presentation.

Public NotInheritable Class VariableManager
Private Sub New()
End Sub

Public Shared ReadOnly Property PagePropPageTitle() As String
Get
Return GetPropertyByName("PagePropPageTitle")
End Get
End Property

Public Shared Function GetPropertyByName(ByVal name As String) As String
Dim values As NameValueCollection = GetAllValues()
Return values(name)
End Function

Private Shared Function GetAllValues() As NameValueCollection
Dim values As NameValueCollection =
CType(HttpRuntime.Cache("AllValues"), NameValueCollection)
If values Is Nothing Then
'retrieve values from xML file and put them in the values

HttpRuntime.Cache.Insert("AllValues", values, New
System.Web.Caching.CacheDependency("XML_FILE_PATH" ))
End If
Return values
End Function
End Class
you can then access the values directly from the user control without any
need to "communicate" with the page via:

VariableManager.PagePropPageTitle

Basically the GetAllValues parses the XML file and stores the values into a
NameValueCollection (key => value) and uses caching so it doesn't always
have to parse it. The NameValueCollection is wrapped inside easy-to-use
methods, alal PagePropertyPageTitle.

Hope this helps :)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/index.aspx - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"darrel" <no*****@hotmail.com> wrote in message
news:ee**************@TK2MSFTNGP14.phx.gbl...
Karl has helped me in the past in regards to communicating between controls and pages:

http://www.openmymind.net/communication/index.html#3.1

I ended up going down the interfaces path and actually got that working
fairly well, but, then, in the end, decided that was overkill and that it
would perhaps make more sense to start over using a much more simpler
communication between pages.

So, this is what I have:

getVariables.ascx
otherControl.ascx
page.aspx

getVariables reads an xml file and sets a variety of strings. We'll use this generic one:

public pagePropPageTitle as string
'logic to read xml and set the value

on page.aspx, I want to get the pagePropPageTitle value and then send that
onto otherControl.ascx

Is there a way to grab that value directly? I can't just go
getVariables.pagePropPageTitle because that isn't declared. Is there another way to go about this?

Looking at the above tutorial, it talks about how to do this using
dynamically loaded controls. Is that the only way? If so, is there anything wrong with dynamically loading all of your controls via the codebehind?

-Darrel

Nov 19 '05 #2
> Having said that, perhaps we discussed this previously, but it seems to me
that maybe getVariables.ascx shouldn't be a user control. Does it display
anything or does it simply get variables from XML and make them available?
It just parses the XML and gets a bunch of variables that are then used by
the other UCs on the page. It has no presentation itself at all.

I feel kind of stupid now, but it never occured to me to just use a class.
Duh. That makes the most sense. Argh! ;o)

Thanks for rattling my head and seeing that option!

To be more specific (in hopes of fully explaining this) I'll try to give a
better overview of what is going on:

We have a 'mini CMS' tool that we've built. This tool can spit out an XML
file that stores a variety of information:

- pageTitle
- contentID
- customContent
- linkName
- etc
- etc

Basically, a bunch of settings for each individual page that appears on the
site. From this XML file, we can easily build our navigation system using
some XSLT. On each page load, I want to pass the current pageID and then
have it read the other variables needed by the rest of the controls.
you can then access the values directly from the user control without any
need to "communicate" with the page via:

VariableManager.PagePropPageTitle


Hmm...let me think about that. That might work quite nicely.

The only catch is that this class is being used by 10 different 'template'
pages all with unique XML files, not to mention unique page IDs.

For instance, I may have this:

site1.aspx?page=12
- variablemanagerclass

site2.aspx?page=15
- variablemanagerclass

I could obviously just pass these from the page into the class, though:

getVariables(site, page)

but then I'm looking up the information on each request...which would seem
to be a bit of a performance hit.

Is there anything wrong with using the class to pull up individual values on
each aspx page?

For instance, on site1.aspx:

pageTitle = getVariables(passSiteID, passPageID)

then, dynamically load the control that uses page Title and pass it that
way?

-Darrel
Nov 19 '05 #3
> I could obviously just pass these from the page into the class, though:

getVariables(site, page)
If it'were would probably provide an override which knows what the current
site and current page is. Actually, that information seems like it would be
useful to many controls and should easily be available. I'm doing something
similar and I use a "SiteContext" class to hold information, the SiteContext
is a singleton which looks a lot like:

public class SiteContext{
#region Fields and Properties
private Section section;
private HttpContext context;
private int documentId;

public int DocumentId
{
get { return documentId; }
set { documentId = value; }
}
public Section Section
{
get { return section; }
set { section = value; }
}
public static SiteContext Current
{
get
{
if (HttpContext.Current.Items["SiteContext"] == null)
{
HttpContext.Current.Items.Add(contextKey, new SiteContext());
}
return (SiteContext)HttpContext.Current.Items["SiteContext"];
}
}
#endregion
#region Constructors
internal SiteContext()
{
context = HttpContext.Current;
string[] segments = context.Request.Url.Segments;
string sectionName = GetSectionName(segments);
sectionName = sectionName.ToUpper();

documentId = Utility.ParseQuerystring("documentId", -1);
if (documentId != -1)
{
type = DocumentType.DocumentViewer;
}
section = SectionUtility.GetAllSections().FindByPath(section Name);
if (section == null)
{
section = SectionUtility.GetDefaultSection();
}
}
}
}
You can then access this "SiteContext" from any control/page via:
SiteContext.Current.DocumentId

of course you would have a "PageId" and a "SiteId"...tends to make your
controls "smarter" and less far more self-reliant (ie, less communication
between controls/page, since all the core pieces of info are in the
SiteContext which they can readily access).

As for your multiple site thing...I'd tie in what I've said above, but
change the GetAllValues() I provided previously to take a siteid/pageid and
cache based on that....

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/index.aspx - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Having said that, perhaps we discussed this previously, but it seems to me that maybe getVariables.ascx shouldn't be a user control. Does it display anything or does it simply get variables from XML and make them available?
It just parses the XML and gets a bunch of variables that are then used by
the other UCs on the page. It has no presentation itself at all.

I feel kind of stupid now, but it never occured to me to just use a class.
Duh. That makes the most sense. Argh! ;o)

Thanks for rattling my head and seeing that option!

To be more specific (in hopes of fully explaining this) I'll try to give a
better overview of what is going on:

We have a 'mini CMS' tool that we've built. This tool can spit out an XML
file that stores a variety of information:

- pageTitle
- contentID
- customContent
- linkName
- etc
- etc

Basically, a bunch of settings for each individual page that appears on the site. From this XML file, we can easily build our navigation system using
some XSLT. On each page load, I want to pass the current pageID and then
have it read the other variables needed by the rest of the controls.
you can then access the values directly from the user control without
any need to "communicate" with the page via:

VariableManager.PagePropPageTitle


Hmm...let me think about that. That might work quite nicely.

The only catch is that this class is being used by 10 different 'template'
pages all with unique XML files, not to mention unique page IDs.

For instance, I may have this:

site1.aspx?page=12
- variablemanagerclass

site2.aspx?page=15
- variablemanagerclass

I could obviously just pass these from the page into the class, though:

getVariables(site, page)

but then I'm looking up the information on each request...which would seem
to be a bit of a performance hit.

Is there anything wrong with using the class to pull up individual values

on each aspx page?

For instance, on site1.aspx:

pageTitle = getVariables(passSiteID, passPageID)

then, dynamically load the control that uses page Title and pass it that
way?

-Darrel

Nov 19 '05 #4
This is one of those things that was so easy with ASP that has become much
more of a project than I had anticipated in .net ;o)

Thanks, Karl. I'll have to sit down with your sample code for a bit and see
if I can absorb it.

-Darrel
Nov 19 '05 #5

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

Similar topics

1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
5
by: Peter | last post by:
Hello! I Have a problem. I try to use asp.net page-wide variable but it is not working. I declare my boolean variable (eg. bool done=false) in the same place where page's web controls are...
4
by: N. Demos | last post by:
Hello, I'm learning ASP.NET, and am having a strange problem with some example code from the book I'm using. The code increments and displays the value stored in a session variable when the "Add"...
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...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
2
by: Thief_ | last post by:
I'm using the following code to read in a web page as HTML: ' Download the web page using the chosen update method. Dim sr As New...
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
9
by: The Ax | last post by:
I want to read this array backwards. http://hiscore.runescape.com/index_lite.ws?player=grimmstriker Its not my site so I cant find the arrays name instead of: 1319710,847,1620107 -1,-1,-1 i...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.