Pass datatable to different page to be diaplyed in gridview - asp.net

I have an aspx page with a gridview control. The code-behind for this page fills a datatable and then displays the datatable in the gridview. I then need to pass the datatable to a separate aspx page and display it again in a gridview on THAT page. I'm not sure how to go about this.
On the second page, in the code-behind, I'll have a simple function that receives a datatable and displays it in a gridview, but what is the best way to do what I'm trying to do? I need the gridview to display on the original page, but also display on the new page.
So... somehow display the gridview on page1, then pop open the new page and pass the datatable to my function? At a loss here.

You can try using Sessions, once you display the GridView for the first time, just save the DataTable it loaded into a Session, something like Session["griddata"] = myDataTable. Then in the other page just pull it out of the Session likeDataTable myDataTable = (DataTable)Session["griddata"]; then bind it to the grid.

Related

Navigate to a certain page of a GridView from an other aspx page

I have a website with 2 pages, Master.aspx and Detail.aspx.
In Master.aspx there is a Gridview and when I click on a certain row it will redirect to Detail.aspx.
The Gridview inside Master.aspx implements "pager size", so when the user is inside Detail.aspx, I want them to be able to return to Master.aspx, precisely in the same page Gridview. To solve this problem I'm using the following JavaScript:
history.go(-n)
This allows the user to return to the correct Gridview page, however, it doesn't update the information inside the Gridview (naturally when the user makes changes in Detail.aspx).
How can I update the information inside the Gridview appropriately?
Moreover, I noticed that when I click the page buttons of the Gridview they use a __doPostBack() function:
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Page$3')"
style="color:White;">3</a>
Is it possible to use the __doPostBack function from Detail.aspx, to redirect to a specific page of the Gridview in Master.aspx?
When redirecting back to Master.aspx from Default.aspx you can have it go to the correct page index by passing the page index number through the url
Response.Redirect("Detail.aspx?id=" + gridview1.PageIndex.ToString()).
On Master.aspx you would then have to set the content of gridview1 then do
gridview1.DataBind();
gridview1.PageIndex = Request.QueryString("id")

GridView not visible in ContentPlaceHolder on content page

I am trying to create a page that uses a MasterPage to organize content. On my content page, I have created a text search, which will filter a GridView presentation of data.
When I use the databinding in a non-master page setup, it works perfectly. But, as soon as I drop the controls into a contentplaceholder on my page, it no longer displays any results.
Any suggestions would be greatly appreciated.
try these
debug your page with breakpoints and be sure your gridview datasource is not null
make sure you have registerd the controls in master page
Sometimes it is due to css. also check css of content page
try to make controls on content page not just copy and paste them from another page
To fatch the values from previous page store values in session and on second page
DataSet ds = (DataSet)(Session["id"]);
Gridview1.DataSource = ds;
Gridview1.DataBind();

GridView when the page is refreshed

I have a GridView and a Button in a web form.
I change SelectCommand of SqlDataSource when user click on the button.
Then the result has been shown in GridView. But when the page is refreshed, results changes. For example when user wants to change the page of GridView.
Why?
Seems that your data is bounded again to grid for every Page load.
Please make sure that the the setting the datasource and databinding is not done on every page load..
Put your code in (!IsPostBack) like this:
if(!IsPostBack)
{
Gridview1.DataSource=yourDataSource;
GridView1.DataBind();
}

Grid View View state

I'm Using a GridView in my Application.
My GridView has a checkbox column and when the checkbox is clicked, the page is redirected to an other form.
When I return to previous from, nothing comes and GridView data is lost.
I used session and it's working fine, but I want that checkbox which was clicked to also maintain there state.
Is there better method to do this?
Upon redirection to the next page, Iterate your Gridview to update your Datatable with a checked checkbox column and then set that datatable to a session variable. I am assuming that you are already storing your source Datatable to a session.
Sorry, but no other magic work around !!!

Getting the bound Datarow of a Datagrid on postback in ASP.NET 4

I want to find out the bound datarow or datatable of a datagrid on postback. I have a ButtonColumn in the grid. On click of the button, I'm trying to determine the datarow so that I can access the primary key and pass it to another page. The primary key is not bound and therefore not visible in the row.
For example I have a list of Customers, with an edit button. On clicking of the edit button, I want to open CustomerEdit.aspx?id=10. I am able to trap the click event on the server side in the DataGridCustomer_ItemCommand event. But am not able to get access to the datarow of the e.Item.ItemIndex.
On postback I'm NOT binding the datagrid. On accessing DataGridCustomer.DataSource, I get "Nothing". Is there a way to get the DataSource or the Datarow?
Thanks
You can't. The DataBind method your are calling the page load method, when it's not a postback will iterate through your source to build controls.
After that, the source is not kept...only the resulting controls.
I suggest you to build the url to the edit page instead of relying on postback.
In few words, create a HyperLinkColumn in your datagrid, and specify the text fields and datafields to build the full url.

Resources