ASP.NET - Save In-line Gridview Rows without Postback
Asked By SP on 05-Jul-15 01:23 PM
I do have a gridview which has some data.
I have Edit, Cancel & Save buttons outside the Gridview.
On clicking "Edit" button, all the rows in gridview appears in edit mode in textboxes.
I want to save the data in all rows on clicking the "Save" button.
The tough part is my page does not have a post back. So, when I click "Save", it calls the onLoad event and binds the datagrid again. After that it calls the button_click event which ultimately has lost the updated data and has the one which got bound from database.
How should I solve this?
I have following methods in the code behind
protected override void OnInit(EventArgs e)
protected override void OnLoad(EventArgs e)
protected override void OnPreRender(EventArgs e)
SP replied to Robbe Morris on 05-Jul-15 10:26 PM
Hi Robbe,
I think I got lost or confused with page life-cycle event here. I didn't go through the whole stuff yet, but when I joined this project I was told that we are not using page_load and Page.Ispostback because we are not posting back the page to server to make it a little faster. But I didn't understand this. It has all code to load the data into the controls in OnLoad or OnInit event. Each time when something is clicked or changed, it reloads all controls again and retains the values from hidden fields. Doesn't that put more load on the page!
Since I started working in .net, I usually bound all controls in page_load event with a check Page.Ispostback which avoids database call each time. I don't know why they are saying that it's not a good idea.
Robbe Morris replied to SP on 06-Jul-15 07:27 AM
If they are not posting back, then none of the server side events you are accustomed to using will be fired. It sounds like they are using JSON calls or AJAX. Which, makes HTTP calls back to your page via JavaScript without posting the whole thing back. I think you need more clarification on the tactics they want to use.
SP replied to Robbe Morris on 07-Jul-15 12:19 PM
but what should I do in this case then.
Right now, when I click my save button, goes to onLoad method and reloads the data from db to gridview and I'm loosing the values in the textboxes. If I put the Gridview databind logic in (isPostback) check, it doesn't load at all. the gridview stays empty.
Robbe Morris replied to SP on 07-Jul-15 01:24 PM
If you are dynamically adding textboxes to your grid control, then yes, you lose them on postback "unless" you recreate the controls server side first when it is postback. Classic case in the postback model and dynamically creating controls.
This article discusses what happens and one old suggestion for getting around it.
https://www.nullskull.com/a/1617/aspnet-dynamic-textbox--dropdownlist-control-viewstate-to-store-namevaluecollection.aspx
Ideally though, this is where the AJAX/JSON type model comes into play. You trigger client side javascript code to transmit your data (and only the data-not posting back the page/control) from the grid back to the page. ASP.NET PageMethods would be the simplest way to move forward if you were not already doing an ASP.NET MVC app.