简体   繁体   中英

how can I save GridView rows and call them same after Postback?

I am learning asp.net and I have a dynamic GridView , meaning the GridView rows change after a search, meaning its SelectCommand changes.

My aim:

1 - To save the GridView and its rows resulting of a search,

2 - and get the same GridView (results of a search) after any number of pages postbacks.

How can I achieve this?

I think you need to check the Page.IsPostBack property before you bind data to your GridView. Try something like this:

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
      GridView1.DataSourceID = "SqlDataSource1";
      GridView1.DataBind();
  }
}

You need to bind your code in page_load inside Page.IsPostback Property like

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
             // Initially bind your grid view like
                GridView1.DataSource = "DataSource";
                GridView1.DataBind();
            }
        }

After Search

In searching scenarion you have to pass the user input as a parameter and search according to user input and call you DataBind method again to refresh your grid view related to user input.

Temporary Storage

Firstly you need to create a temporary table in the database, Your database structure will be look like the fields you want to show on grid or related to search query selected value.

Example

if you are selecting Name,Address,Phonenumber,Salary etc fields from search query then you have to create those all fields into your temporary database table.

Now you have Question.

1) Where to store data into Temprary table.

When you are getting value from database filter by search then run a insert query and put those data into your temporary table as well for furthur use.

2) Table must be persistent from page to page.

You are saving data into your database as a temporary table so it must be persistent.

Now you have the searched data into your temporary table so whenever you want to show searched data, bind your grid view with temporary table means use temporary table name into your query like

Select * from temporarytablename

Hope you understand.

Hope it works.

The ability of an ASP.Net control like a GridView to retain state after Postbacks to the same page is inherent if you enable ViewState on the page and control, provided that the user doesn't leave the .aspx page.

As others have mentioned, one of the common reasons why ViewState doesn't work correctly is if you inadvertently rebind new / different data to the GridView after a postback - you can avoid doing this by checking for !IsPostBack (ie bind the initial data to the GridView only on the first GET access to the page, or rebind it explicitly eg if the user changes his / her search filter etc).

If you need to retain the original search results after the user navigates away from the page containing the GridView, you will need to cache the state elsewhere, such as in SessionState , although storing large amounts of data in SessionState isn't recommended. In this case, you might instead save the filter or query that the user ran to obtain the data in SessionState , rather than the actual data returned from the search.

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