State storage in GridView - asp.net

gridview has 20 pages. The user is see first gridview page . He decide to apply highlite style using jquery on few row . Now he move to second page . He apply style this page too. Now when he go back to first page, he cannot see row with style he apply before he move to page 2.
How to store state of the rows style when user moving on pages? Perdon my English

Use a cookie to store highlighted rows on a given page. Hook up to a event on which highlighting takes place (click?) and add a code to rewrite a cookie with currently selected rows. When you will be printing the table just read the cookie and restore selections based on the value stored in it.

I would save the highlighted row id's in a hiddenfield. Pageing causes a Postback! Also you need some javascript code to re-highlight the fields.

If can afford to make the solution less efficient, I would change the highlight method so that in runs server side with a post back instead of using JavaScript and then put the GridView in a UpdatePanel.
Also remember to use DataSource object (e.g. ObjectDataSource) instead of binding your data manually using .DataBind() in the code behind. This should ensure that you don't rebind the data on every post back.

Related

ASP.net Using a Session Varriable to move a GridView SelectedDataKey to another page

Help please!
I am pulling out what little hair I have left over this and can't find an answer.
I am attempting to move the selected row in a grid view to another page and use the SelectedDataKey or the primary key value of the selected row to indicate what data should be displayed in a grid view on another page. This is the last little bit of a school project that I can't figure out.
What I am doing on the first page is to use a button click event to set a session variable that looks something like this. Session["Select"] = GridView1.SelectedDataKey; The button will then send the user to the next page. Response.Redirect("CustomerAccounts.aspx");
Once on the next page I want to use the primary key selected on the first page to show only the data with a coresponding value on the second page by using a where statement in the next gridview. So I am setting the [CustomerID]=? Session("Session["Select"])
Am I setting up the where correctly in the second gridview?
I like setting up such things with querystrings.
I made a video tutorial on youtube doing just that a while back
http://www.youtube.com/watch?v=HRjZ_0JpO2M&list=UUgtKyNvllU9E6c2Mi8OtzCQ&index=5&feature=plcp
I also made a tutorial showing how to load the detail gridview nested in the master gridview with jquery
http://www.youtube.com/watch?v=nJp14o_1wZQ&list=UUgtKyNvllU9E6c2Mi8OtzCQ&index=4&feature=plcp

GridView with a custom button

Specifically, I am using DevExpress 10.1 for their APSxGridView. I have a grid with a command column. I plan on using two of the standards: new and edit. I would like to create a custom button that will take a value from within the current row and redirect to another page using that value as part of the query string.
I have seen some suggestions to create a hyperlink column to do this, but I would like to have all of these commands in a single column. I can see solutions for this if I weren't constrained by leaving all buttons within a single column. Is this something that maybe I could assign during HtmlDataCellPrepared?
Thanks.
If it's anything like the standard .net GridView I would create a template field. In a template field you can put whatever you want in there. I have done this when I need an asp.net control (a button, hyperlink, etc) instead of the standard grid buttons and commands. You can then access controls and the datasource during the OnRowDataBound event handler and customize the button during run time if need be.

Modifying GridView data using Jquery doesn't persist back to Server events

So i have an ASP.NET GridView control in which each column is a 'BoundField'. I did not create a TemplateField(ItemTemplate/EditItemTemplate), because i was planning on using JQuery to convert the BoundField row to a 'Edit' field by just converting the text in each cell to a textbox or textarea when that row was clicked. This is done all client side.
So far all the client side stuff works great. But I have a 'Save' button on each row of the grid that triggers an eventhandler on the server side. In that server side method, i try to grab values of that current row, but they are all the OLD cell values before I modified the fields/data using jquery/javascript.
When i iterate through each cell of the row, it's the same state as it was when it was rendered.
So what i'm trying to understand is. Why do all of this fancy javascript/jquery stuff when the state of the Grid stays exactly as it was when it was rendered when posting back to the server ?
someone please shed some light on this!!!
Thanks!
The GridView controls uses ViewState to persist its state between postbacks. When you do a postback it loads its previous state from the ViewState. Changes that you do via client-side Javascript are ignored. This is simply how the control works.
If you want to keep using the GridView control with inline editing, the easiest option would be to move to edit mode via a postback.
Another option is to have the fields in each row to begin with, only hidden. Then, you could show them via Javascript. You'll be able to access the values on the server.
Yet another option is not use a postback but an Ajax call, so that the grid is not re-rendered when you click the button. But that means you would have to manually collect the values from the grid via client-side code and pass it to the server.

Why do I need to bind GridView on every postback to make click events from columns to work

Ok, i have a gridview and on page load, i dynamically add a column to it that contains a link control. this link control has a onclick event associated with it so that when its clicked, i want to do some processing. Now I have noticed that if I just bind the grid the first time (i.e. if(!IsPostBack) and have enableviewstate for the grid to be true, clicking the link in that column, doesnt trigger the onclick event. But if I bind the grid on every postback, everything seems to be working..does anyone know the reasoning behind it?
It happens because you're dynamically adding the column, so on every postback the column has to be created. What you may want to do is to look at Creating a Custom Column.
May be creating the row Id solves your problem. Check out this link and the post marked as answer.
http://forums.asp.net/p/1471128/3408069.aspx#3408069
Regards
Vinay

How do I trigger a PostBack with arguments from Javascript?

This is what I'm doing: Using jquery, I'm popping up a "form" over the page that lets the user search for branches of the company and select the one they want. When the form pops up, they can type in a textbox, and it will do AJAX requests back to the server to return the top n results for what they've entered, and those results will be put into a list for them. I want the user to be able to select one by clicking a link that says "select" or something, and at that point I want it to do a PostBack have the Branch Selector control that this is in change it's SelectedBranch property to the newly selected branch. I've got this all working right now with a hard coded list of LinkButtons, but how do I do the same thing with a dynamic list of links inserted with jquery?
Look at the HTML that gets emitted for your hard coded LinkButtons. You'll see that each one calls the JavaScript __doPostBack function when clicked. I believe this function takes two arguments: a control ID and an extra command argument you can use for your own purposes.
I would suggest adding a single control to the page whose only job is handling events for the dynamic links. Then, when you are creating the links with jquery, make each one call __doPostBack, passing the event handling control's ID for the first argument and some other string for the second argument that identifies which link was clicked. In the Click event for the handling control, look at the second argument value and do what you need to do.
The short answer is... you don't.
ASP.NET relies on the Viewstate for the current state of the controls, including items in a DropDownList or similar control. Dynamically updating a list on the client will not modify the viewstate, so will not be available on the back end.
The general workaround for this is to just add a hidden field which updates/stores the current selection via js on the client side. Then read it from this field on the backend rather than List.SelectedValue.

Resources