Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session variables not set until second time page is hit

Status
Not open for further replies.

belle9

Programmer
Nov 3, 2004
87
US
I'm trying to access a session variable. It loses its value the first time I hit a page, but if I hit that page twice before doing anything(click the same link twice), it works.

So, page default.aspx, has the below code:
Code:
 Session("ProdID") = ProdID
 Response.Redirect("/pages/Products.aspx")

If I start at default.aspx and click on the image that calls the above code, when it redirects ProdID is NOTHING. However, if when I start at default.aspx, and I reload it by clicking a link that directs right back to itself, ProdID is the right value when redirected. It seems somehow I have to hit the page twice to load up the session...DOes that make any sense?
 
Post your code for the button and your page load

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Here it is, the originating page:

Code:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        myConnection = New SqlConnection(conString)
        myConnection.Open()
        Dim bottleshot As String
        mycommand = New SqlCommand("getDefaultProducts", myConnection)
        mycommand.CommandType = CommandType.StoredProcedure

        myReader = mycommand.ExecuteReader

        While myReader.Read
            bottleshot = myReader.Item("defaultImagePath")
            dlDefaultProducts.DataSource = myReader
            dlDefaultProducts.DataBind()

        End While

        myConnection.Close()

    End Sub

    Public Sub btnDefaultProd_Clicked(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

        Dim objButton As ImageButton
        objButton = CType(sender, ImageButton)
        Dim ProdID As String = CType(sender, ImageButton).CommandArgument
        Session("ProdID") = ProdID
        Response.Redirect("/pages/Products.aspx?prodid=3")

    End Sub

Here's the image:
Code:
	<asp:imagebutton id=btnDefaultProd onclick=btnDefaultProd_Clicked ImageUrl='<%#Container.DataItem("defaultImagePath")%>' CommandArgument='<%# Container.DataItem("productsID") %>' Runat="server" ImageAlign="Top">
									</asp:imagebutton>
 
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
if not page.ispostback then
        myConnection = New SqlConnection(conString)
        myConnection.Open()
        Dim bottleshot As String
        mycommand = New SqlCommand("getDefaultProducts", myConnection)
        mycommand.CommandType = CommandType.StoredProcedure

        myReader = mycommand.ExecuteReader

        While myReader.Read
            bottleshot = myReader.Item("defaultImagePath")
            dlDefaultProducts.DataSource = myReader
            dlDefaultProducts.DataBind()

        End While

        myConnection.Close()
end if
    End Sub

try adding this if not page.ispostback statement.

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Anyone? Any ideas?? This is really aggravating me.
 
But it works the second time the page is hit, just not the very first time. Wouldn't that mean it does work?

I'll try server.transfer..
 
server.transfer causes other things to mess up..

This:
Code:
<%@ Register TagPrefix="uc1" TagName="header" Src="/includes/header.ascx" %>

gives this error:
"The virtual path '/includes/header.ascx' maps to another application, which is not allowed.
 
I think this is because the page_load fires first no matter what. Your session has not been set because of the order, page_load -> button_click (where your session is).

I am having this problem too, but I think that is where the problem lies.

Also response.redirect does carry a session state.

I am trying to upload a picture to a server with a button click. Then after the picture is uploaded I want it to be displayed. Check out this page if you are interested.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top