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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect to a specific sub within a different page 1

Status
Not open for further replies.

belle9

Programmer
Nov 3, 2004
87
US
I am trying to redirect the user when they click on an image on the homepage to go to the specific product listing page. I'd like it to go directly to the sub that is defined in the product page that deals with displaying the product...How can I do that? I'm not using any parameters in the URL, so that's out, unless of course there's no other way.

Thanks!
 
Not sure why you would want to do it this way but here it is:

ASPX PAGE:
<asp:Button id="Button1" runat="server" Text="Button" CommandArgument="101"></asp:Button>



ASPX CODE:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ProdID As String = CType(sender, Button).CommandArgument
Session("ProdID") = ProdID
Response.Redirect("ProdDetail.aspx")
End Sub




ACCEPTING PAGE:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Select Case Session("ProdID")
Case "101"
MySubForProduct101()
Case "102"
MySubForProduct102()
End Select
End Sub

Keeping it Simple
 
Thanks. I'll try to explain why I want to do it that way, and maybe you'll tell me I can do it much easier another way....

My homepage has rotating product images, which should direct the user to the product page with the item details displayed. This product page exists already, and is invoked when browsing through product categories and clicking down the menu...The product details are displayed in the same page as the main product page, there is no separate page named 'productdetails.aspx' or the like. That is why I would need to go directly to a specific sub with the products.aspx.vb, otherwise if I redirect to the page load sub the wrong info will be displayed.

I hope that makes sense.. My other option would be to create a new page specifically designed to display the details when linked from the homepage, but I'd rather not do that.
 
I would be tempted to create a new page (I know you said you would rather not!) but it will give you a lot more control, especially if you decide to make any design changes. Just a personal preference though as I think it makes good sense to do so.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
RTomes,

I can't seem to get the session variable to work. It's 0 every time...Am I missing something?

To test it, here's what I did:
Code:
Public Sub btnDefaultProd_Clicked(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

        Dim objButton As ImageButton

        Dim ProdID As Int16 = CType(sender, ImageButton).CommandArgument
        Session("ProdID") = ProdID
        Dim prodid2 As Int16 = Session("ProdID")
        Response.Redirect("/pages/Products.aspx")

    End Sub

ProdID is populated correctly, but prodiId2 is not.
 
It will always be zero. You declare an ImageButton when you do:
Code:
Dim objButton As ImageButton
but this Image Button has no command argurments hence when it is convert to an Int16 it is set as zero. See the following line in RTomes first post:
Code:
<asp:Button id="Button1" runat="server" Text="Button" CommandArgument="101"></asp:Button>
or save yourself a lot of hassle and do it properly with another page.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
The image button does indeed have a commandargument:

Code:
<asp:ImageButton onclick="btnDefaultProd_Clicked" ImageUrl=<%#Container.DataItem("defaultImagePath")%> id="btnDefaultProd" Runat="server" CommandArgument='<%# Container.DataItem("productsID") %>'>
										</asp:ImageButton>
 
Also, I set it back to String, but the session variable is now NOTHING.
 
Apologies - that code wasn't included in your post so I has to assume you hadn't added it.

If ProdId is populated correctly by the following line:
Code:
        Dim ProdID As Int16 = CType(sender, ImageButton).CommandArgument
then why do you even need to make a second instance of it in ProdID2?

Also, you could check if it is being written correctly to the session variable by adding Tracing to your page as this will show you all session variables.


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
I'm using ProdID2 to see if it's getting populated. Changing from INt to String populated ProdID2, but when I get to products.aspx, and try to initialize a var with the session variable, I get NOTHING.

Code:
  Dim productsID As String = Session("ProdID")
 
Nothing shows up in the session named prodid..

Code:
 <trace enabled="true" requestLimit="10" pageOutput="true" traceMode="SortByTime" localOnly="true" />
    <sessionState 
            mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="false" 
            timeout="20"  />
 
Why don't you look at it in a different way then. You could always redirect to the same page but add a querystring to it. i.e.
Code:
Response.Redirect("/pages/Products.aspx?ProdID" & ProdID)
Then on the page, if the Querystring("ProdID") exists then show that product.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Ok here's a silly question: I would like to check if the variable exists in the url. Somehow whatever way I try I get Object reference not set to an instance of an object.
Here is what I have:
Code:
Dim Context As System.Web.HttpContext = System.Web.HttpContext.Current
        If Context.Request.QueryString("ProdID").Length = 0 Then
 
You can just use:
Code:
        If Request.QueryString("ProdID") <> "" Then

        End If

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
It's been bugging me that the session variable wasn't working, and here's what I found:

On the first load on the referring page, the session variable is Nothing. However, if I merely hit the same page twice, or do a refresh, then the variable gets saved. It seems that the page needs to be refreshed to get the session to hold variables.
Does that make any sense??

 
Nope! [smile]

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
For some reason Session Variables are not saving correctly.
That is big issue that needs to be corrected or there is no sense in trying to develop on that machine the way it is. Start a new web project.

Add a label.
<asp:label runat=server id=Label1 />

In the code behind add:

Page_Load(....)
Session("MyTest")="Yes it works"
End sub

Page_PreRender(...)
Label1.text= Session("MyTest")
End Sub

Try retyping when you run into this stuff. Sometimes you can copy and paste errors and hidden illegal characters.
Remember if it is viewstate("MyTest") does not equal viewstate("Mytest"). It is case sensative. Session vars are not.

Keeping it Simple
 
The test works fine...it gets the value of session("MyTest") on the first load of the page. I still have the issue though in my main project!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top