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

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

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


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

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

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

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

Replies have been disabled for this discussion.