开发者

getting a gridview and dropdownlist to remember their states after navigate away and back button scriptmanager

开发者 https://www.devze.com 2022-12-26 17:13 出处:网络
i have a page filling a gridview which is all working fine. the grid is basically the result of a search .. the filters for which are a number of dropdowns and a couple of textboxes

i have a page filling a gridview which is all working fine.

the grid is basically the result of a search .. the filters for which are a number of dropdowns and a couple of textboxes the data from the grid and dropdowns are saved in the session and the whole page lives inside an updatepanel

when i navigate away from the page (as it happens by clicking a link inside the grid) and then back button to it, all the droppers are back to their unselected values and the grid is nowhere to be seen..

i understand that开发者_开发技巧 this is because of the scriptmanager doesnt do 'standard' postbacks so the browser doesnt realise what has happened.

however i have set the EnableHistory to true in the scriptmanager

is there an easy way to get this to remember without dropping the updatepanel/scriptmanager?

also to complicate things further the scriptmanger /updatepanel is actually in a master page. so not really sure how i can get the navigate bits to work in the scriptmanager.. clearly i am a bit confuised and any help that someone could provide would be happily received

thanks nat


Setting EnableHistory only tells the ScriptManager to be ready for you to use the server-side support. It is still up to you to tell the ScriptManager how to actually work with it. It doesn't matter that the UpdatePanel is on a MasterPage. Same goes with the ScriptManager. As long as you can get the references you need via a public property / FindControl, you'll be fine. I've done this several times with the ScriptManager in the MasterPage and handling the event

Here is a blog post I wrote about 2 years ago. It is a bit out of date as you don't need the Extensions Preview, but the rest should be relevant enough. When you are in an async postback and binding the gridview, you have to add state to a history point, and let the ScriptManager store it. This is where you would freeze the state of the filters. Then, you also need to have an event handler for the Navigate event. This is where you pull the state out, use it to set controls to what they should be, and bind your grid.

The code samples are in vb.net, but please let me know and I can convert to c#.


Another answer that worked well for me:

-on the gridview, add this: OnPageIndexChanging="Gridview2_PageIndexChanging"

-in the code (I'm using VB), add:

Protected Sub Gridview2_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    Session("page") = e.NewPageIndex
End Sub

-in the Page_Load sub, add:

GridView2.PageIndex = Session("page")

That was much simpler than the other solutions I found out there...

0

精彩评论

暂无评论...
验证码 换一张
取 消