我有一个C#Multiview,每当我点击最后一个菜单项时,它会给我一个错误并停止在Visual Studio 2010中运行。错误是'ActiveViewIndex被设置为'8'。它必须小于View控件'8'的当前数量。对于动态添加的视图,请确保在Page_PreInit事件之前或之中添加它们。'
<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>
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);
}
任何帮助都是超级的。
答案 0 :(得分:1)
您有9个菜单项和8个视图
MultiView1.ActiveViewIndex = 8
会失败,因为ActiveViewIndex
从零开始(正确实施)
您可以先评估MultiView1.Views的长度,然后设置ActiveViewIndex
int index = Int32.Parse(Menu1.SelectedValue)
if (MultiView1.Views.Count > index)
{
MultiView1.ActiveViewIndex = index
}
也许您应该检查索引是否不是-1
且大于或等于0
。