Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 11th, 2005, 09:44 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Changing a session variable BEFORE the page loads

Hi

I'm hoping that someone can help me with the following problem.

I have a website with various tours. Each tour is in it's own folder, and the folder name is the tour code (ie, folder name "AB" = "Abaco"). Essentially what I need to happen is the following:

1) User clicks on a page
2) ASP.NET code determines the path of that page, and determines what folder it is sitting in. By doing this it determines what tour the user is looking at, ie, gets the tour code (I've done this part)
3) ASP.NET code checks to see if this tour code is the same value as the tour code currently specified in the Session variable [Session("strTourCode")]
4) If it is the same, everything is happy and the page content then loads
5) If it is NOT the same, Session variable [Session("strTourCode")] is replaced with the new tour code value just obtained.
6) Page content then loads, using the NEW session variable

The biggest problem I have, is that I have tried to update the Session Variable in Page_Init() and Page_PreRender() and Page_Load() but have not achieved the desired result. The most that I can get to happen is that once the page is loaded, if I then refresh the page, it will reload the page using the new Session Variable. I can't seem to update the Session Variable FIRST so that it uses that value for the rest of the page loading.

The reason I am using a session variable, is that I have various controls on the page (and some in user-controls) which require the tour code value (ie, breadcrumb control that needs to know what folder the user is in to start it's path back to home, a header control that needs to know the tour code, before it can query the database for the tour name, price and departure). I thought it was easiest to store the tour code as Session Variable because it needs to be accessed in several places within page, and on every page. When a user clicks on another page (but in the same folder) by quickly checking to see if this folder is still the same as the session variable, I don't have to query my database unnecessarily...

Maybe I am wrong in this assumption and there is a better way to do this OR is there a way that I can update the Session Variable BEFORE the page loads so that I can use the new Session variable value within the page...

I really hope this makes sense to someone.....

Thanks
:D


... please help me....

 
Old May 11th, 2005, 09:50 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Well, where is the code that you are using to load the page content? It must fall after you set the session value.

-Peter
 
Old May 12th, 2005, 03:33 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter,

Thanks so much for your reply. The code is as follows:

My glabal.asax file contains the following:
    Sub Session_Start(Sender As Object, E As EventArgs)
        Dim strTourCode As String
        Session("TourCode") = strTourCode
    End Sub

My page content is as follows:
<%@ Page Language="VB" Debug="True" %>
<%@ Register TagPrefix="DTW" TagName="HeadFoot" Src="/NEWDTW/commonfiles/headfootPanel.ascx" %>
<%@ Register TagPrefix="DTW" TagName="Destination" Src="/NEWDTW/commonfiles/destPanel.ascx" %>
<%@ Register TagPrefix="DTW" TagName="Left" Src="/NEWDTW/commonfiles/leftPanel.ascx" %>
<%@ Register TagPrefix="DTW" TagName="Right" Src="/NEWDTW/commonfiles/rightPanel.ascx" %>
<script language="VB" runat="server">
    Sub Page_Load()
        Dim strPath As String = Page.Request.Path
        Dim intStart As Integer = strPath.LastIndexOf("/") - 2
        Dim intLength As Integer = 2
        Dim strNewString As String = strPath.Substring(intStart,intLength)

        If Session("TourCode") <> strNewString Then
           Session("TourCode") = strNewString
        End If
    End Sub

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>New Template</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
    <form runat="server">
        <DTW:HEADFOOT id="headfootPanels" runat="server"></DTW:HEADFOOT>
        <DTW:DESTINATION id="destPanel" runat="server"></DTW:DESTINATION>
        <DTW:LEFT id="leftBoxes" runat="server"></DTW:LEFT>
        <DTW:RIGHT id="rightBoxes" runat="server"></DTW:RIGHT>
    </form>
</body>
</html>

The DTW:DESTINATION user control contains two custom controls that call the Session variable (ie, one creates a breadcrumb, the other creates a tour header). The DTW:LEFT also contains one custom control (which creates links to other relevant tours). Hence, all three need the most recent session variable.

When looking at the "Page Lifecycle" I thought that the Page gets initialized first, hence I tried to update the session variable first in the Page_Init() thinking that thought would occur before any of my controls (which call the session variable) are rendered? When this didn't work, I tried the Page_Load(), but it had the same result.

Apologies for not thinking earlier to include the code. I haven't included the code for the custom controls, I hope this isn't a problem (they're kinda long). Also apologies for any delay in replying - it's the time difference.

Thank you for your help once again.

 
Old May 12th, 2005, 08:27 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I've been looking at this further, and it now appears that the session variable IS changing (adding a Response.Write(Session("TourCode")) in the Page_Init() has shown me this. However, my Custom Controls are still showing the old session variable. Here is an example of my custom control (which creates a breadcrumb):

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.XML
Imports System.XML.XPath
Imports System.Text
Imports System.IO
Imports.System.HTTPContext

Namespace Navigation

        Public Class BreadCrumb
            Inherits Control

            Private _strTourCode As String

            Public Sub New()
            _strTourCode = Current.Session("TourCode")
            End Sub

            Function createBreadCrumb(tourCode As String) As String
                Dim strBreadCrumb As String
                Dim objXMLDoc As XPathDocument = New XPathDocument(Page.MapPath("~/NewDTW/navigation/sitemap.xml"))
                Dim objXPNav As XPathNavigator = objXMLDoc.CreateNavigator()
                    objXPNav.MoveToRoot()
                   'Moves to the node of this tour
                    objXPNav.MoveToID(tourCode)

                   'writes this tour name (without the link)
                    strBreadCrumb = objXPNav.GetAttribute("tourName","")

                   'while there is still a parent node to move up to, that has an attribute (ie, nothing above <sitemap>)
                    While objXPNav.MoveToParent()
                        If objXPNav.HasAttributes Then
                            strBreadCrumb = "<a class=""breadcrumb"" href=""" & objXPNav.GetAttribute("tourUrl","") & """>" & objXPNav.GetAttribute("tourName","") & "</a> &gt; " & strBreadCrumb
                        End If
                    End While
                Return strBreadCrumb
            End Function


            '************************************************* ************************************************** ****
            '*RENDER THE TOUR OPTIONS BOX AND THE PAGE LINK CONTENTS
            '************************************************* ************************************************** ****
                Protected Overrides Sub Render(writer As HtmlTextWriter)
                    writer.write("<div id=""breadcrumbpanel"">")
                    writer.write(createBreadCrumb(_strTourCode))
                    writer.write("</div>")
                End Sub
        End Class


Does anyone have any ideas why my controls aren't getting the latest Session varaible?

 
Old May 12th, 2005, 08:50 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Ah, it is clear now.

When your page is requested, ASP.NET loads the classes required. Then it starts the lifecycle.

What's happening is that your user control is asking for the session value when the class is created but BEFORE the page starts to load. You are setting the session value in the init or load of the page, but the user control has already gotten the session value.

Try moving the Session("TourCode") to where you actually use it in the control code. You should find that it will then work because that code is getting called after the page and controls have been initialized and loaded.

-Peter
 
Old May 12th, 2005, 09:31 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter

Thanks once again for your reply - I really appreciate the help. I've done as you suggested, and you're quite right. Once I have it in the controls where I call the session variable, it works like a treat. But that leaves me with one more question...

I've got three different controls which use the session variable, so at the moment I've inserted the code into each of those controls. Is there any way that I can add it in just one particular control/place so that I don't need to add it to all three?

I was thinking it would be cleaner to just do it once, and not have to do it three times (which is what I was originally hoping to achieve by putting in the the Page_Init()).

I was thinking, is it possible to create a class which contains the function to check the tour page and updates the session variable, and then just import that into each of the three controls which use the session variable??? Would that work?

My page structure, at the moment is like this:

<%@ Page Language="VB" Debug="True" %>
<%@ Register TagPrefix="DTW" TagName="HeadFoot" Src="/NEWDTW/commonfiles/headfootPanel.ascx" %>
<%@ Register TagPrefix="DTW" TagName="Destination" Src="/NEWDTW/commonfiles/destPanel.ascx" %>
<%@ Register TagPrefix="DTW" TagName="Left" Src="/NEWDTW/commonfiles/leftPanel.ascx" %>
<%@ Register TagPrefix="DTW" TagName="Right" Src="/NEWDTW/commonfiles/rightPanel.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>New Template</title>
</head>
<body>
    <form runat="server">
        <DTW:HEADFOOT id="headfootPanels" runat="server"></DTW:HEADFOOT>
        <DTW:DESTINATION id="destPanel" runat="server"></DTW:DESTINATION>
        <DTW:LEFT id="leftBoxes" runat="server"></DTW:LEFT>
        <DTW:RIGHT id="rightBoxes" runat="server"></DTW:RIGHT>
    </form>
</body>
</html>

Inside DTW:HEADFOOT:
<HEADER:HEADERPANEL id="headerPanel" runat="server" />
<FOOTER:FOOTERPANEL id="footerPanel" runat="server" />

Inside DTW:DESTINATION:
<NAVBOXES:BREADCRUMB id="breadCrumb" runat="server" /> ***
<NAVBOXES:DROPDOWNNAVIGATION id="navigation" runat="server" />

Inside DTW:LEFT
<NAVBOXES:RELEVANTTOURS id="relevantTours" runat="server" /> ***
<LEFTBOXES:TOUROPTIONS id="tourOptions" runat="server" /> ***
<LEFTBOXES:PAGEOPTIONS id="pageOptions" runat="server" />

Those with *** are the ones that call the Session variable.

Perhaps I've looked at it too much today... I have a funny feeling that somewhere in the Wrox books there's an example where common functions are placed in their own class and are imported into various controls. Does this sound possible, or do you have any advice?

THANK YOU ONCE AGAIN!!!!!!

 
Old May 12th, 2005, 01:26 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'm a little confused. If you moved the code that gets the value out of the session into where you need it and that makes it work, what code is it that you are saying has to be repeated in each control?

You'll have to have something that gets the value in each control. I see on the other thread that you are trying to create a helper class that performs that path check and such, my understanding is that you do this once on the page, then the user controls the value in session that you place in that helper logic. So I'm not sure what it is that you are thinking needs to be repeated.

Did you interpret my prior suggestion such that you put that big chunk of code that does the path check in each control? Or just the line from the original Sub New() that gets the value from the session?

You should only need to perform the path check once, in the page load. Then the controls can just reference the session value without performing the whole check.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Session variable or other soulfly73 Classic ASP Basics 4 December 4th, 2008 12:52 PM
Print a page without changing page setup nrajeshatwork Servlets 1 May 23rd, 2007 10:09 AM
drop down list opens dynamically when page loads robear Javascript How-To 5 April 21st, 2006 10:05 AM
Random News Topic when the page loads morpheus Classic ASP Databases 4 November 21st, 2003 01:38 PM
Generate a random article when page loads morpheus SQL Server ASP 1 October 28th, 2003 10:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.