Solved

pass viewstate in querystring to create a 'back to results' page

Posted on 2008-10-16
8
1,161 Views
Last Modified: 2013-12-17
I am creating an asp.net (linq to sql) app so user can search list of products by entering keyword.  The product are diplayed as images in listview in an item results page.  User can click on an image in the listview to get details about the product on a product page.  
Whilst on this product page I want a "back to results" link so when user clicks this it return to the previous results page and page is populated using the original keyword search criteria.

So I want to do the following..

1.  "Product" Results page is populated with data using "keyword"
2.  Add this keyword to viewstate and pass it onto the "Product" page together with the id of the product via querystring.
3.  On the product page - a "back to results" link that uses the viewstate to go back to the original search results page.


Ideally I want users to be able to bookmark the results page -

Thanks

Jules
 
0
Comment
Question by:mugsey
  • 4
  • 3
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Why dont you use repeater? Or if you use datagrid, you could provide paging and sorting etc.
0
 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
There are several solutions for your case, but Viewstate isn't an appropriate option because: Viewstate is specific to the page it pertains to  and is only available when that page posts back to itselft.

With Server.Transfer, you could look at ways to pass the previous page's
 state, or HttpContext Items collection. In ASP.NET 2.0 you have cross - page postbacks and the Page class exposes a PreviousPage property. Not ViewState

A simple solution is using Session. Store this key value in Session and in your search result, look up for this session value when the page is NOT postback, the load the list accordingly. remember to set this Session to null when not use.

you will have to put also the querystring value
(let say like: product.aspx?id=111&result=true)

YOu will only let them return to result page, when you navigate to this ProductID from that page: the indication is result=true (or Session["searchkey"] != null)

2nd: Navigate from your ProductPage back to the other by using PreviousPage and System.Transfer

the URL to go back : Redirect of NavigateURL
Response.Redirect("list.aspx?searchkey=" + ((TextBox)PreviousPage.FindControl("tbKey")).Text)

Then your list page have to be able to handle querystring searchkey as well.

Hope this helps,

JINN






Session["searchkey"] = tbProductSearch.Text;
 

in your Page_Load

{

   if(!IsPostBack)

   {

         If(Session["searchkey"] != null)

             ... call the function to bind your List

   }

}

Open in new window

0
 

Author Comment

by:mugsey
Comment Utility
HI Folks

Thanks for your replies but it is a requirement that I CANNOT use session state, also I cannot use pager controls.  You see they want to be able to copy the URL of the search results and book mark it if need be in browser.

0
 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
Well,

Have you try my Server.Transfer suggestion?

Another try can be, you also pass the search key word when you go to product details (even the page number). So the navigateURL to the product details can look like:

productdetails.aspx?id=bmw111&searchkey=bmw&page=2

In the product page, this way as soon as you find in the searchkey querystring is not empty, then you know it came from a search page (not direct URL type in), so you can visible the BACK TO SEARCH accordingly. The return URL now become:

productlist.aspx?searchkey=bmw&page=2

Bookmark solved!

Then in the list page, process the searchkey queryString the same way as you do in the textbox.

We have that in some of our project too.

Hope this helps

JINN
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mugsey
Comment Utility
Thanks JINN

Could you give me an example please using the explanation you give above??  

THis one........productdetails.aspx?id=bmw111&searchkey=bmw&page=2
0
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
Comment Utility
Ok,

In your list result page. When you bound item to the listview (in the itemTemplate you might have set the URL for some link button (or image) which navigate to the Product details page right?

Set the event ItemDataBound then in side that ItemDataBound of the ListView method you set the navigate url to the detail page the way you want them (pass querystring etc...)

Please check the syntax a gain, but the idea is like that.

Hope this help

JINN

PS: for more information:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx



<ItemTemplate>

    <tr>

        <td>

            <asp:HyperLink ID="product" runat="server" Text='<% #Eval("ProductID")%>' NavigateURL="~/ProductDetails.aspx">View product

            </asp:LinkButton> 

        </td>

        <td>
 

// ProductList.aspx.cs

protected void ProductsListView_ItemDataBound(object sender, ListViewItemEventArgs e)

  {

    if (e.Item.ItemType == ListViewItemType.DataItem)

    {

      //get the current productID

      ListViewDataItem currentItem = (ListViewDataItem)e.Item;

       DataKey currentDataKey = this.MyListView.DataKeys[currentItem.DataItemIndex];
 

      //get the current search key

      string searchKey = tbSearch.Text;

      // set the navigate URL

      HyperLink product = (Label)e.Item.FindControl("product");

      product.NavigateURL = "~/ProductDetails.aspx?id=" + currentDataKey.Value.ToString() + "&searchkey=" + searchKey;

    }

  }

Open in new window

0
 

Author Comment

by:mugsey
Comment Utility
Thanks Jinn

I appreciate your comments!
0
 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
Glad to help ^^

JINN
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now