Error adding a tab to a TabPanel

Last post 09-25-2008 11:13 AM by chinnivasudeva. 7 replies.

Sort Posts:

  • Error adding a tab to a TabPanel

    02-13-2007, 12:24 PM
    • Member
      7 point Member
    • theWheel
    • Member since 01-06-2006, 7:36 PM
    • Pensacola, FL
    • Posts 5

    When I try to create a new tab for a TabPanel it throws the following error:
     

    Specified argument was out of the range of valid values.


    Parameter name: index

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
    Parameter name: index

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

    Parameter name: index]

       System.Web.UI.ControlCollection.get_Item(Int32 index) +2057550

       AjaxControlToolkit.TabPanelCollection.get_Item(Int32 index) +29

       AjaxControlToolkit.TabContainer.LoadClientState(String clientState) +216

       AjaxControlToolkit.ScriptControlBase.LoadPostData(String postDataKey, NameValueCollection postCollection) +74

       AjaxControlToolkit.TabContainer.LoadPostData(String postDataKey, NameValueCollection postCollection) +32

       AjaxControlToolkit.ScriptControlBase.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +11

       System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +718

       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3776


     

     The problem seems to be that you can only create tabs dynamically once on a user event such as a button click. However you can create as many as you want at the initial attempt it is the subsequent inserts that fail.

     EXAMPLE

    XHTML markup:

     <%@ Page Language="C#" AutoEventWireup="true" CodeFile="testpage.aspx.cs" Inherits="testpage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

            <asp:ScriptManager ID="ScriptManager" runat="server" />
            
            Try this example, you can only create tabs dynamically once on a user event such as a button click, yet you can create as many as you want in one shot.<br />
            Try this page now. Then uncomment the code in the Page_Load event and try again.<br />

            Number of Tabs to Create<asp:TextBox ID="txtNumberOfTabs" Text="5" runat="server" />
            <asp:Button ID="btnAddTab" Text="Add Tab(s)" OnClick="btnAddTab_Click" runat="server" />

            <ajaxToolkit:TabContainer ID="tabContainer" runat="server">
                <ajaxToolkit:TabPanel runat="server" HeaderText="The First Tab">
                    <ContentTemplate>
                    </ContentTemplate>
                </ajaxToolkit:TabPanel>
            </ajaxToolkit:TabContainer>
        
        </div>
        </form>
    </body>
    </html>

     Code behind:

     using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class testpage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //for (int i = 0; i < Int32.Parse(txtNumberOfTabs.Text.ToString()); i++)
            //{
            //    AjaxControlToolkit.TabPanel thePanel = new AjaxControlToolkit.TabPanel();
            //    thePanel.HeaderText = "tab" + (tabContainer.Tabs.Count);
            //    thePanel.ID = "tab" + (tabContainer.Tabs.Count);
            //    tabContainer.Tabs.Add(thePanel);
            //}

        }

        protected void btnAddTab_Click(Object sender, EventArgs e)
        {
            for (int i = 0; i < Int32.Parse(txtNumberOfTabs.Text.ToString()); i++)
            {
                AjaxControlToolkit.TabPanel thePanel = new AjaxControlToolkit.TabPanel();
                thePanel.HeaderText = "tab" + (tabContainer.Tabs.Count);
                thePanel.ID = "tab" + (tabContainer.Tabs.Count);
                tabContainer.Tabs.Add(thePanel);
            }
        }
    }


     Configuration: 

    Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

    AJAX.net: 1.0.61025.0

    AJAXToolkit 1.0.10201.0
     

     

  • Re: Error adding a tab to a TabPanel

    02-13-2007, 3:44 PM
    • Contributor
      2,019 point Contributor
    • Jason Hill
    • Member since 04-23-2006, 3:39 AM
    • Sydney, Australia
    • Posts 479

    I have found that you have to add the tabs back in every postback and that you have to do this in Page_Init.

    Jason

  • Re: Error adding a tab to a TabPanel

    02-13-2007, 8:06 PM
    • Member
      6 point Member
    • mmahrle
    • Member since 02-03-2007, 12:03 AM
    • Posts 3

    I have seen a similar problem.  I have a TabContainer control that I populate with TabPanels programmatically based on the conteents of a SQL database.  The initial display of the page works great.  However, if I cause a postback (in my case by a button) the TabContainer LoadClientState function gets called and throws the "Argument of of range of valid values" exception because the client state string indicates that there are x number of active tabs in the tab control, but they don't exist yet because the page_load hasn't occurred.

    I'm hoping someone has an approach to this that will work.  I'm thinking of just adding a bunch of tabpanels and only enabling the ones I need, but that seems like a kludgy way to do it.

     

  • Re: Error adding a tab to a TabPanel

    02-13-2007, 8:10 PM
    Answer
    • Contributor
      2,019 point Contributor
    • Jason Hill
    • Member since 04-23-2006, 3:39 AM
    • Sydney, Australia
    • Posts 479
    Are you recreating the tabs in Page_Init every postback?  It is the same as with any dynamically created server controls...they need to be recreated every postback because their definition does not exist in the aspx file.
  • Re: Error adding a tab to a TabPanel

    02-14-2007, 11:06 AM
    • Member
      7 point Member
    • theWheel
    • Member since 01-06-2006, 7:36 PM
    • Pensacola, FL
    • Posts 5

    That makes sense.

     

     When I moved the tabPanel generation to the Page_Int it did not throw the error on every postback.  However this means that I would need to save the state of my dynamic tab with each postback, which is not possible given the nature of the data I am displaying.  On the bright side this has made me rethink the way my pages are going to work so they are now far more usable. 


    In hind sight, what I was trying to was not going to work anyway as it would require some really icky code with very little benefit to the end user.  

    Thanks for your help.


    Nathan Wheeler
     

  • Re: Error adding a tab to a TabPanel

    02-14-2007, 3:03 PM
    • Member
      6 point Member
    • mmahrle
    • Member since 02-03-2007, 12:03 AM
    • Posts 3

    Yes, I was recreating all the tabs, but in Page_load, not Page_Init.  That solved my problem with the exception.

    Thanks

  • Re: Error adding a tab to a TabPanel

    04-25-2007, 9:28 PM
    • Member
      316 point Member
    • ans_anish
    • Member since 10-10-2005, 11:25 AM
    • Posts 95

    you said you are recreating tabs, but how did you maintain the state of the tabs: headertext, contorls inside the tabs etc.

    need help

     

  • Re: Error adding a tab to a TabPanel

    09-25-2008, 11:13 AM
    • Member
      10 point Member
    • chinnivasudeva
    • Member since 09-19-2008, 5:34 AM
    • Bangalore
    • Posts 5

    Draw the dynamic tabs in Page_Init and don't draw any tabs created dynamically in page load. In page load create all the child control for dynamic tabs. I think this will solve your problem.

Page 1 of 1 (8 items)