Click here to Skip to main content
15,509,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have already read all the articles about persisting dynamic controls data, but I can not find the right one for my problem.

I am trying to import a variable count excel files to APSX page. I chose to display excel files via gridviews and continuously save to a List<gridview> (for later handling).

But I can't figure out the problem with postback calling, respectively to persist List<gridview> when an OnClick event occurs.
I have tried Page_PreInit, ViewState, ... But I do not know how..

Please, don't anyone know how to solve this?

Thank you very much.

What I have tried:

ASPX page:
<pre><asp:Panel ID="pnl_grids" runat="server"></asp:Panel>
<asp:Button ID="bttn_CreateNewOrder" runat="server" Text="Test" OnClick="bttn_CreateNewOrder_Click" />

private List<GridView> gvObjList = new List<GridView>();
foreach (var file in dirParts.GetFiles())
                string fileName = Path.GetFileName(file.FullName);
                string fileExtension = Path.GetExtension(file.FullName);

                //excel bind
                SparePartsOrderAHUMAN sp_AHUMAN = new SparePartsOrderAHUMAN(fileExtension, filesOrder.Order_PARTS_Path + "\\" + fileName);

                //create gridView
                GridView objGV = new GridView();
                objGV.ID = "gv_" + Path.GetFileNameWithoutExtension(file.FullName);                
                objGV.RowDataBound += new GridViewRowEventHandler(objGV_RowDataBound); //formating gridView
                objGV.DataSource = sp_AHUMAN.dtSparePart;

                //CSS style
                objGV.Attributes.Add("class", "gv_SpareParts");
                objGV.AlternatingRowStyle.CssClass = "gv_SpareParts_AltrowStyle";
                objGV.HeaderStyle.CssClass = "gv_SpareParts_HeaderStyle";
                objGV.RowStyle.CssClass = "gv_SpareParts_Rowstyle";

//add to page and List, on the end

protected void bttn_CreateNewOrder_Click(object sender, EventArgs e)
        ..code after button click..
Updated 13-May-20 7:29am

1 solution

When you make a request to your page, a new instance of your page class is created. It handles the request, sends the response to the user, and is then thrown away.

Any values you store in a field within your class will not persist between requests.

You might be tempted to try to store the list in the session. But that will simply cause a memory leak in your application - controls are not meant to persist beyond a single request.

And using a static variable wouldn't work either. The single list would be shared by all requests from all users of your application. You'd have severe thread-safety issues. And you'd still have the memory leak.

You need to recreate the grids each time your page is loaded. You could potentially use view-state[^] to help, although this would dramatically increase the size of the rendered page and the request payload.
Share this answer
MitchCZ 14-May-20 5:25am    
Thank you Richard.
It seems there is no good solution.

I do not know if it is a clear programming solution, but I will try to create one gridview in aspx page for all excel files, iteratively add records and separate it by empty row in grid. If I think correctly..
What I need to do next is select some rows and than save to the data table in the database.

Btw. Is there the same problem with ASP.NET MVC where everything is created dynamically?
Richard Deeming 14-May-20 6:17am    
MVC doesn't have a concept of "controls", and doesn't use view-state.

Other than that, the same comments apply - you either have to recreate the grids every time the action is called, or store the data somewhere in memory on the server. Storing the data in memory will likely cause problems, especially if you have more than a couple of users.

And static variables are still almost always wrong in a web application. :)
MitchCZ 14-May-20 8:07am    
That's true, static variables are a way to hell..(convinced :))

So, I should the excel files import to the database first and then manage the data with gridviews on the page standardly.
However, there is still a problem with the variable number of excel files. It depends on the the user's input. So it's not clear to me how to create a variable count of gridviews on the one page, select a rows from all grids at one time and continue by click on the button (save selected rows to new data table and forward to the next page..) It seems that I have to save the checked rows (true/false) to the database as a special column first and only then press the button..??
Richard Deeming 14-May-20 8:18am    
It looks like you're loading the grids from Excel files. Why not store the information required to identify the files to load in the session, and use it to reload the files on each page load? That information should be significantly smaller than the loaded data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900