ActiveViewIndex is being set to '0'

2.2k Views Asked by At

Alright... Searched online, followed about 100 answers, nothing is working so I am obviously not understanding something.

I have a set of tabs that are dynamically created. When the user clicks one of the tabs, it should change to that tab and its content. However, every single time I try to do so, I get the following error message:

ActiveViewIndex is being set to '0'. It must be smaller than the current number of View controls '0'. For dynamically added views, make sure they are added before or in Page_PreInit event.

Here's the ASP.NET code:

<table width="90%" align="center">
    <tr>
    <td>
    <asp:Panel ID="buttonPanel" runat="server" />
        <asp:MultiView ID="ProfileView" runat="server">
        </asp:MultiView>
    </td>
    </tr>
</table>

Here's the C# code:

Button[] _tabs; //tabs for the profile
View[] _views; //views inside of the MainView
string[] _tabTitles = { "Main", "Website Stats", "About", "Other", 
                         "Wall", "Posts", "Topics", "Pictures" };

protected void Page_PreInit(object sender, EventArgs e)
{
    CreateTabs();
}

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < _tabs.Length; i++)
    {
        buttonPanel.Controls.Add(_tabs[i]);
        ProfileView.Controls.Add(_views[i]);
    }
    _tabs[0].CssClass = "Clicked";
    ProfileView.ActiveViewIndex = 0;
 }

protected void CreateTabs()
{
    _tabs = new Button[_tabTitles.Length];
    _views = new View[_tabTitles.Length];

    for (int i = 0; i < _tabs.Length; i++)
    {
        _views[i] = new View();
        _views[i].ID = _tabTitles[i] + "View";

        _tabs[i] = new Button();
        _tabs[i].CssClass = "Initial";
        _tabs[i].BorderStyle = BorderStyle.None;
        _tabs[i].Text = _tabTitles[i];
        _tabs[i].Click += TabClick;
    }
}

protected void TabClick(object sender, EventArgs e)
{
    for (int i = 0; i < _tabs.Length; i++)
    {
        if ((sender as Button) == _tabs[i])
        {
            _tabs[i].CssClass = "Clicked";
            ProfileView.ActiveViewIndex = i;
        }
        else
            _tabs[i].CssClass = "Initial";
    }
}

I did do debug and tried to see if TabClick was being called, but apparently it crashes the page before it even gets there. If I comment out:

ProfileView.ActiveViewIndex = 0;

I can click one tab, it switches over successfully, but then when clicking another tab it crashes with an error like:

ActiveViewIndex is being set to '2'. It must be smaller than the current number of View controls '0'. For dynamically added views, make sure they are added before or in Page_PreInit event.

What am I doing wrong?

1

There are 1 best solutions below

0
On

It seems that I work on something for hours and hours, and then immediately after I post a question to somewhere (which I rarely do), I answer my own question. So annoying.

Anyway, I did the following (for anyone else who is having the same problem):

Added the following field:

MultiView _profileView;

Inside of Page_PreInit():

_profileView = new MultiView();
_profileView.ID = "ProfileView";

And then I simply added _profileView to the buttonPanel after the for in Page_Load.