Here is one way to have data load only when a TabPanel is clicked. In my example I have an aspx page with a TabContainer, two TabPanels both with their own ObjectDataSources that fill GridViews. The first tab’s ObjectDataSource and GridView will run on the page load and that is fine since it would be the first thing a user sees. The overall idea is to have the ObjectDataSource for the second tab to not run on the page load and then have the GridView on the second tab databind only when the tab is clicked (this happens in ActiveTabChanged).
First set the datasource to not select on the intial page load like:
Protected Sub odsTab2_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles odsTab2.Selecting
If Not Page.IsPostBack Then e.Cancel = True
End Sub
-or-
protected void odsTab2_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (!Page.IsPostBack) e.Cancel = true;
}
Then set the TabContainer’s autopostback property to True and add in the following code behind (note that the ClientID will vary based on your code):
Protected Sub TabContainer1_ActiveTabChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabContainer1.ActiveTabChanged
Dim tabID As String
tabID = TabContainer1.ActiveTab.ClientID
If tabID = "ctl00_ContentPlaceHolder1_TabContainer1_tpTab2" Then
gvTab2.DataBind()
End If
-or-
protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
{
string tabID;
tabID = TabContainer1.ActiveTab.ClientID;
if (tabID == "ctl100_ContentPlaceHolder1_TabContainer1_tpTab2")
{
gvTab2.DataBind();
}
}
End Sub