asp.net Tab Control causes index out of bounds error on callback
I have a simple application with a GridView bound to a LinqDataSource and a DetailView that displays a row from the GridView when it's selected. This works as expected. But now I'm placing the GridView and DetailView in separate TabPanels in an asp.net ajax Tab Control.
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:TabContainer ID="CourseFinder" runat="server" ActiveTabIndex="0">
<asp:TabPanel ID="ResultsTab" runat="server" HeaderText="Results" >
</asp:TabPanel>
<asp:TabPanel ID="DetailTab" runat="server" HeaderText="Detail">
</asp:TabPanel>
</asp:TabContainer>
and my code behind file
protected void Page_Load(object sender, EventArgs e)
{
CourseFinder.Tabs[0].Controls.Add(Results);
CourseFinder.Tabs[1].Controls.Add(DetailsView1);
}
When I run the page I'm getting
[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than
the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +7483656
System.Web.UI.WebControls.GridViewRowCollection.get_Item(Int32 index) +13
_Default.Results_SelectedIndexChanged(Object sender, EventArgs e) in c:\Documents and
Settings\Administrator\My Documents\Visual Studio
2008\WebSites\WebSite1\Default.aspx.cs:35
...
The line of code getting the error is the one attempting to get the SelectedIndex from the GridView and update my DetailView.
GridViewRow row = Results.Rows[Results.SelectedIndex];
I stepped through the code with the debugger and SelectedIndex is not negative and it's not larger than the number of rows in the GridView. So开发者_高级运维 I'm confused why simply placing the controls inside a Tab is causing this error?
Are you adding the panels dynamically? [Karel]
Yes...
Is there another way to get the tabs to display without adding them dynamically? I couldn't get them to display otherwise [Matt Phillips]
Don't do that! It makes things much more complicated. You could add your GridView/DetailsView to an UserControl and add this to the ContentTemplate of the TabPanel.
Here is a good tutorial on how to lazy-load TabPanels: http://mattberseth.com/blog/2007/07/how_to_lazyload_tabpanels_with.html
From the toolkit (just add tabpannels in your aspx page; the error will be solved):
<ajaxToolkit:TabContainer runat="server"
OnClientActiveTabChanged="ClientFunction"
Height="150px">
<ajaxToolkit:TabPanel runat="server"
HeaderText="Signature and Bio"
<ContentTemplate>
...
</ContentTemplate>
/>
精彩评论