I have a C# Multiview and everytime I click on the last menu item it gives me an error
I have a C# Multiview and everytime I click on the last menu item it gives me an error and stops running in Visual Studio 2010. The error is 'ActiveViewIndex is being set to '8'. It must be smaller than the current number of View controls '8'. For dynamically added views, make sure they are added before or in Page_PreInit event.'
CODE
<div>
<asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick" Orientation="Horizontal" style="left: 0px; position: relative; top: 18px; font-family:Arial;" Height="38px" Width="800px" >
<StaticMenuStyle HorizontalPadding="0px" VerticalPadding="0px" />
<StaticSelectedStyle BackColor="#EAEAEA" BorderColor="#000000" />
<Items>
<asp:MenuItem Text="about us" Value="0" Selected="True"></asp:MenuItem>
<asp:MenuItem Text="events" Value="1"></asp:MenuItem>
<asp:MenuItem Text="contact us" Value="2"></asp:MenuItem>
<asp:MenuItem Text="patio" Value="3"></asp:MenuItem>
<asp:MenuItem Text="customers" Value="4"></asp:MenuItem>
<asp:MenuItem Text="family" Value="5"></asp:MenuItem>
<asp:MenuItem Text="swans" Value="6"></asp:MenuItem>
<asp:MenuItem Text="swim" Value="7"></asp:MenuItem>
<asp:MenuItem Text="bonus" Value="8"></asp:MenuItem>
</Items>
<StaticHoverStyle BackColor="Silver" />
<StaticMenuItemStyle BorderColor="#EAEAEA" BorderStyle="Solid" BorderWidth="1px" />
</asp:Menu>
</div>
<div style="width: 800px; height: 450px; background-color: #EAEAEA; border:1px solid #000000; padding:10px 10px 0px 10px; font-family: Arial;" >
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server"></asp:View>
<asp开发者_高级运维:View ID="View2" runat="server"></asp:View>
<asp:View ID="View3" runat="server"></asp:View>
<asp:View ID="View4" runat="server"></asp:View>
<asp:View ID="View5" runat="server"></asp:View>
<asp:View ID="View6" runat="server"></asp:View>
<asp:View ID="View7" runat="server"></asp:View>
<asp:View ID="View8" runat="server"></asp:View>
</asp:MultiView>
</div>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
MultiView1.ActiveViewIndex = 0;
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
MultiView1.ActiveViewIndex = Int32.Parse(Menu1.SelectedValue);
}
Any help would be super duper.
You have 9 menu-items and 8 views
MultiView1.ActiveViewIndex = 8
would fail since the ActiveViewIndex
is zero-based (as you correctly implemented)
you could evaluate the length of the MultiView1.Views first and then set the ActiveViewIndex
int index = Int32.Parse(Menu1.SelectedValue)
if (MultiView1.Views.Count > index)
{
MultiView1.ActiveViewIndex = index
}
perhaps you should check if index is not -1
and larger or equal than 0
.
精彩评论