I have a gridview with data on a content page.i need to get the datasource of the gridview as a datatable on master page button click.following is my code
string DicPageName = Page.GetType().Name;
GridView gv1;
DataTable DT;
if (DicPageName == "dictionaries_abs_aspx")
{
gv1 = (GridView)ContentPlaceHolder2.FindControl("GridView1");
if (gv1 != null)
{
if (gv1.Visible)
{
DT = new DataTable();
DT = (DataTable)gv1.DataSource;
}
}
}
but DT returns null.plz help
You have mentioned that the code above is located in a button-click event handler. That is always on postbacks.
The datasource of any web-databound control is null on postbacks. You need to query the datasource(for example a database) again if you want it (or store it in the Seession).
Out of interest: why do you need it? Maybe there’s a better approach.