简体   繁体   中英

Back to List Previous Next control in .NET issue

I need help because I didn't find out a solution for a project where I am using MVVM. In one hand I have a Default.aspx that contains an UpdatePanel with two user controls. The first control is a FilterControl with some Fields to filter. The second control is a GridView that allows pagination and populates data from the database when I press the button search in my FilterControl. Let's say that the query returns 100 records. So if I set the PageSize to 20 results I have 5 pages in my GridView ok?

On the other hand I have a Details page with a FormView that recieves an Id parameter of an item from the GridView and shows the information about it. Just above of asp:FormView code I have another user control that gets the data filtered from the gridview in a Session variable throught the corresponding ViewModel, and calculates which items should be next and previous so I have one "Back to List" link and "Previous" and "Next" links. The previous and next links loads correctly. They are LinkButtons with OnClick event that makes Response.Redirect(PrevLink); for example, where PrevLink is the link where the page redirects ~/details.aspx?Id=5).

I am controlling the navigation asynchronously throughout an eventHandler and HistoryEventArgs so when I press the Back button of the browser everything is fine and I get the desired results in default.aspx page. Whenever a postback is fired in default page, when changing the page index of the gridview, for example, or executing a new query in the filter, I call the AddHistoryPoint of ScriptManager and save several keys and values (Filter fields, Number of page, etc).

The problem comes when I press the Back to List LinkButton. I wish the data were the same as before going to the detail page. I have Response.Redirect("~/default.aspx") in the OnClick event of this button.

Do you have any ideas in how to manage this?

Thank you very much!!


Copied code from the answer here

Thank you for the answers. @Guvante I don't want to put the "back to list" on client side because when I Edit the Details page the visible property of the navigation control is set to false. I think I didn't express myself correctly. But I don't have the Response.Redirect in the OnClick event of the aspx page. What I have in the source code of the page is OnClick=lkbBackToList_Navigate.

I have it in the codebehind in this way:

protected void lkbNext_Navigate(object sender, EventArgs e)
{
   Response.Redirect(NavigationViewModel.NextUrl);
}

protected void lkbPrevious_Navigate(object sender, EventArgs e)
{
   Response.Redirect(NavigationViewModel.PreviousUrl);
}

protected void lkbBackToList_Navigate(object sender, EventArgs e)
{
   Response.Redirect(NavigationViewModel.BackToListUrl);
}
@msarchet this is some of my code in default.aspx code behind. When I do postback I save the state of the filters, pageindex and so on in session

public event EventHandler<HistoryEventArgs> CaptureClientBrowsing;

protected void Page_OnInit(EventArgs e) 
{
    this.CaptureClientBrowsing += new EventHandler<HistoryEventArgs>(CaptureClientBrowsing_Event); 
}

protected override void SaveDataInSession() 
{
    SaveInSession("FilterViewModel", this.FilteringControl.ViewModel);
    SaveInSession("PageIndex", this.CurrentPageIndex); //CurrentPageIndex is a property with the page index of the gridview 
}

protected void CaptureClientBrowsing_Event(object sender, HistoryEventArgs e) 
{    LoadDataFromSession(e.State); //e.State is a NameValueCollection with the values stored in session for example FilterViewModel has the user control FilteringControl and "PageIndex" has the page index of the gridview    ControlsDataBinding(); 
}

The SaveInSession method just keep Session[key] = object where key is a string as it follows:

string key = string.Format("{0}.{1}.{2}", this.GetType(), nameValue, index); 

an example of key is

"ASP.customers_default_aspx.PageIndex.1"` so `Session[key]=1

If I click in other page (for example the page number 6) of the grid view the value of the key variable would be

"ASP.customers_default_aspx.PageIndex.2"` and `Session[key]=5

Here is where I am stuck. If I browse from the details page to default page clicking back in the browser the CaptureClientBrowsing_Event fires and the controls are loaded correctly and also the values of the filter.

What I want is that when I click the LinkButton "Back to List" the controls and the filter get loaded too. How do I get the NameValueCollection if I don't have the history args? If I click three times to next and then click to Bak to list how do I know the Session values to load?

Maybe there are a lot of questions and this is hard to handle but I've tried to be the most specific I could.

I have Response.Redirect("~/default.aspx") in the OnClick event of this button.

The problem is doing this causes the list page to be loaded as if it were new. I would recommend instead to track the number of pages away from the list and go back to it by setting the onclick client side to history.go(-pages) .

You said it's already working correctly when the user goes from listpage to detailpage then clicks browser Back btn. How is the CaptureClientBrowsing event fired? Can you post code that shows that?

And couldn't you reuse the same mechanism when they click Back To List?

UPDATE: Okay, then there is a need to send additional state information back when you Response.Redirect() to the listpage.

You could use any of the common techniques: pass a querystring parameter, set a cookie, or set another Session variable.

Detect that signal in the listpage Init or Load, LINQ query the Session keys to get maxHistoryIndex, then restore state from Session using keys derived from maxHistoryIndex.

Eg if they clicked to detailpage after having paged through listpage 3 times, maxHistoryIndex=3 and you can restore Session[String.Format("ASP.customers_default_aspx.PageIndex.{0}", maxHistoryIndex)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM