简体   繁体   中英

Databind a gridview on another page

I have a grid view (gridView1) on an webform1.aspx with a button that can trigger a popup window. The popup window links to webform2.aspx that allow users to import an excel file to get data. The data are expected to be show on the grid view on webform1.aspx. However how can I databind the gridView1, once the excel file is loaded?

I have some ideas to do it, not sure if the logic is correct.

webform1.aspx.cs

//tempDT is a DataTable type to store the data of the grid    
Session["data"] = tempDT;
/*Open a popup with webform2.aspx*/

webform2.aspx.cs

//Get data from original data table and add new data get from excel
DataTable tempDT = (DataTable)Session["data"];
/* Add new data from excel to tempDT here */
Session["data"] = tempDT;

But I don't have any ideas to Databind the grid view after that as I cannot call the grid view in webform2.aspx

I am not sure will this help you are not

   GridView grv = ((GridView)this.Page.PreviousPage.FindControl("gridview1"));

but another way could the way you thinking, just make sure to do post back for webform1, like redirect to webform1 and on form load

you can check that if session is not null then bind with session data

What you want to do is:
1. When you click on button it will pop up a window(webform 2 you are saying)
2. Now User Load the data from Excel file.
3. you want to bind that data in Grid on web form1

Ok, now create the process as below mentioned:

  1. Take a session variable, check if it is not null bind the data from it on webform1 as below:
 if (Session["ExcelData"]!=null) { //Code here of binding the grid DataTable dt= (DataTable)Session["ExcelData"]; GridView1.DataSource= dt; GridView1.DataBind(); } 
  1. Now go to second webform's button click on which you are loading the data from excel.
  2. Create a dataTable and fill it with data from excel(the data loaded from excel)
  3. and store that data table in Session as below:

Session["ExcelData"]=dtExceldata;

  1. close the pop up window and call the GridBind Method.

UPDATED:

That's All. Hope this will help you

You have to add the one more grid view in 'ebform2.aspx and put it inside the panel control which help you to hide the gridview. In import button make panel visible so that the grid view with new value will appear at same page.

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