GridView disappears by clicking browser's back button and LinkButton

Hi all,

I have an ASP.NET web form where I have some dropdowns where users can select car related values and based on those values I display car details in GridView in page1.aspx. I have a button inside GridView and whenever I click on a row in GridView, it redirects to page2.aspx and displays the selected car details. When I click the browser's back button or a ASP.NET LinkButton to go to previous page or page1.aspx, the GridView disappears and all the dropdown's have been set to their default values. I am using Ajax ScriptManager and UpdatePanel control. All the dropdownlost controls and a GridView is inside an UpdatePanel.

I found the following code from google search which saves page history info using ScriptManager. When I use the UpdatePanle only for GridView and after clicking the Browser's back button it displays the GridView. But when I use the UpdatePanle for all dropdownslists and including GridView, it won't display the GridView results by clicking the browser's back button. And also by clicking the ASP.NET LinkButton to go to previous page does not display the GridView and all the dropdownlist have their defrault values. Following is the code:

protected void carlistGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ScriptManager1.IsInAsyncPostBack && !ScriptManager1.IsNavigating)
        {
            ScriptManager1.AddHistoryPoint("Index", carlistGridView.PageIndex.ToString());
        }

    }

    protected void ScriptManager1_Navigate(object sender, HistoryEventArgs e)
    {
        string indexString = e.State["index"];
        if (string.IsNullOrEmpty(indexString))
        {
            carlistGridView.PageIndex = 0;

        }
        else
        {
            int Index = Convert.ToInt32(indexString);
            carlistGridView.PageIndex = Index;
        }
    }


Any ideas?
Itudk_2010Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BurniePCommented:
What you want is to keep the selected values in the dropdown lists and the gridview's datasource in between pages.

One technique that I use is to use server's Cache object to retain my values.

Here is what I suggest :

When you change one of the dropdownlist selected value, you are going to the server to populate your gridview.  In that event, you can add these lines (You need to put in the cache every values you want to keep for when you use the back button) :

      Cache[userID.ToString() + "Dropdownlist1"] = DropDownList1.SelectedValue;
      Cache[userID.ToString() + "Dropdownlist2"] = DropDownList2.SelectedValue;
      Cache[userID.ToString() + "GridView"] = Datasource (Same object that you use for carlistGridView.DataSource = thisobject)

Now, you need to replace userID with a value that can identify the current user accessing your page.  Since Cache is a server object, if you give a generic name, every users will share what's in the Cache.  

Now, in the page_load event of that same page, you can add this : (Very important to do this after you have filled your dropdown lists with their values).

      if (!Page.IsPostBack){

        if (Cache[userID.ToString() + "Dropdownlist1"] != null) {
          DropDownList1.SelectedValue = Cache["UserID_Dropdownlist1"].ToString();
        }

        if (Cache[userID.ToString() + "Dropdownlist1"] != null) {
          DropDownList2.SelectedValue = Cache["UserID_Dropdownlist2"].ToString();
        }

        if (Cache[userID.ToString() + "GridView"] != null) {
          carlistGridView.DataSource = (The type of your DataSource)Cache["UserID_GridView"];
          carlistGridView.DataBind();
        }
}
Itudk_2010Author Commented:
BurnieP,

I really appreciate your efforts. But I am bit confused on your technique. I am not using any userid or login stuff. I will provide my code and maybe you can help me with that. I have a button click on page1.aspx where I select locations dates from 4 dropdownlists and then when user clicks on the button, it populates the GridView. Then the user selects a row from gridView which redirects to page2.aspx. Then I want to go back using either browser's back button or ASP.NET LinkButton to see the same ddl selections and same gridView with data as I mentioned in my previous post.
Here I have provided only one dropdown event for example

protected void ddlPickupLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        string pickUpLocation = ddlPickupLocation.SelectedItem.Text;
        Session["City"] = pickUpLocation;
}

protected void btnSearchCars_Click(object sender, EventArgs e)
    {
            GetAvailableCars();

     }

public void GetAvailableCars()
    {
        string city = Session["City"].ToString();
        ArrayList carList = Car.GetAvailableCars(city);
        if (carList == null)
            return;
        carlistGridView.DataSource = carList;
        carlistGridView.DataBind();
    }

Could you be able to make any changes in my code here to reflect your idea?

Thanks.

BurniePCommented:
Okay, I have updated your code to implement my technique.  You can take a look and see if this can work for you.

protected void Page_Load(object sender, EventArgs e)
{
        // Very Important, Your dropdown lists must have been loaded with values before doing this.
        if (Cache[Session.SessionID + "ddlPickupLocation"] != null) {
          ddlPickupLocation.SelectedValue = Cache[Session.SessionID + "ddlPickupLocation"].ToString();
        }
        // Do this for every other dropdown lists

        if (Cache[Session.SessionID + "carlistGridView"] != null) {
          carlistGridView.DataSource = (ArrayList)Cache[Session.SessionID + "carlistGridView"];
          carlistGridView.DataBind();
        }
}

protected void ddlPickupLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        string pickUpLocation = ddlPickupLocation.SelectedItem.Text;
        Session["City"] = pickUpLocation;
}

protected void btnSearchCars_Click(object sender, EventArgs e)
    {
            GetAvailableCars();
    }

public void GetAvailableCars()
    {
        string city = Session["City"].ToString();
        ArrayList carList = Car.GetAvailableCars(city);
        if (carList == null)
            return;
        carlistGridView.DataSource = carList;
        carlistGridView.DataBind();

      Cache[Session.SessionID + "ddlPickupLocation"] = ddlPickupLocation.SelectedValue;
      // Add your other dropdown lists here.
      Cache[Session.SessionID + "carlistGridView"] = carList;
    }




protected void Page_Load(object sender, EventArgs e)
{
        // Very Important, Your dropdown lists must have been loaded with values before doing this.
        if (Cache[Session.SessionID + "ddlPickupLocation"] != null) {
          ddlPickupLocation.SelectedValue = Cache[Session.SessionID + "ddlPickupLocation"].ToString();
        }
        // Do this for every other dropdown lists 

        if (Cache[Session.SessionID + "carlistGridView"] != null) {
          carlistGridView.DataSource = (ArrayList)Cache[Session.SessionID + "carlistGridView"];
          carlistGridView.DataBind();
        }
}

protected void ddlPickupLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        string pickUpLocation = ddlPickupLocation.SelectedItem.Text;
        Session["City"] = pickUpLocation;
}

protected void btnSearchCars_Click(object sender, EventArgs e)
    {
            GetAvailableCars();
    }

public void GetAvailableCars()
    {
        string city = Session["City"].ToString();
        ArrayList carList = Car.GetAvailableCars(city);
        if (carList == null)
            return;
        carlistGridView.DataSource = carList;
        carlistGridView.DataBind();

      Cache[Session.SessionID + "ddlPickupLocation"] = ddlPickupLocation.SelectedValue;
      // Add your other dropdown lists here.
      Cache[Session.SessionID + "carlistGridView"] = carList;
    }

Open in new window

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Itudk_2010Author Commented:
BurnieP,

Perfect, it works fine now. But could you explain the Cache code a little bit? For example the following code?

  Cache[Session.SessionID + "ddlPickupLocation"] = ddlPickupLocation.SelectedValue;
   Cache[Session.SessionID + "carlistGridView"] = carList;
BurniePCommented:
Cache is a Server object that you can use to keep data in memory and in between postbacks.  You use it a little bit like the Session object.  I have found a good article that explains both of them : http://www.west-wind.com/Weblog/posts/1214.aspx .  

With these 2 lines :

Cache[Session.SessionID + "ddlPickupLocation"] = ddlPickupLocation.SelectedValue;
Cache[Session.SessionID + "carlistGridView"] = carList;

I use Session.SessionID to get a unique ID for the current user.  Then I add the control ID to know what value I am setting in the Cache so I can retrieve it easier after.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Itudk_2010Author Commented:
exellent!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.