dimanche 31 janvier 2016

ActiveViewIndex is being set to '0' during postback?

A user is supposed to enter a number click a button and have dynamically added controls with forwards and backwards arrows to switch between the views. I add a view after the btnParseInput is pressed and switch my MultiView to that view. However afterwards if a user clicks the btnBackwards or btnForwards I recieve the following error.

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. Parameter name: value

Master Page:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Assignment03MasterPage.master.cs" Inherits="privetsl_Assignment03_Assignment03MasterPage" %>

<!DOCTYPE html>

<html xmlns="http://ift.tt/lH0Osb">
<head runat="server">
    <title></title>
    <link href="../Content/bootstrap.css" rel="stylesheet" />
    <link href="../Content/Assignment03.css" rel="stylesheet" />
    <script src="../Scripts/jquery-1.9.1.js"></script>
    <script src="../Scripts/bootstrap.js"></script>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Code behind master page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class privetsl_Assignment03_Assignment03MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["_ViewCount"] = 0;
            Session["pageOn"] = -1;
        }
    }
}

Child page:

<%@ Page Title="" Language="C#" MasterPageFile="~/privetsl_Assignment03/Assignment03MasterPage.master" AutoEventWireup="true" CodeFile="Assignment03.aspx.cs" Inherits="privetsl_Assignment03_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div class="container-fluid text-center">
        <div class="jumbotron text-center">
            <h1>Assignment 03</h1>
        </div>
        <div class="mvDiv">
            <div class="fitDiv">
                <span>
                    <asp:TextBox ID="tbUserInput" runat="server"></asp:TextBox>
                    <asp:Button ID="btnParseInput" runat="server" Text="Go!" OnClick="btnParseInput_Click" />
                </span>
            </div>
            <br />
            <asp:MultiView ID="mvFillMe" runat="server"></asp:MultiView>
            <br />
            <div class="fitDiv">
                <span>
                    <asp:Button CssClass="btnBackwards" ID="btnBackwards" runat="server" Text="<" />
                    <asp:Button CssClass="btnForwards" ID="btnForwards" runat="server" Text=">" />
                </span>
            </div>
        </div>
    </div>
</asp:Content>

and the code behind the child page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class privetsl_Assignment03_Default : System.Web.UI.Page
{
    View home = new View();
    View isThisYourNum = new View();
    View GreatestCommonDenominator = new View();
    MultiView _profileView;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            int viewTemp = (int)Session["_ViewCount"];
            if (viewTemp > 0)
            {
                View isThisYourNum = new View();

                /************** 
                 *  Question  *
                 **************/

                int temp = (int)Session["userNum"];

                Label lblYourNum = new Label();
                lblYourNum.Text = "What number did you enter?";

                RadioButton rbYourNumQ1 = new RadioButton();
                rbYourNumQ1.GroupName = "isThisYourNum";
                rbYourNumQ1.Checked = false;
                temp--;
                rbYourNumQ1.Text = (temp).ToString() + "?";

                RadioButton rbYourNumQ2 = new RadioButton();
                rbYourNumQ2.GroupName = "isThisYourNum";
                rbYourNumQ2.Checked = false;
                rbYourNumQ2.Text = Session["userNum"].ToString() + "?";

                RadioButton rbYourNumQ3 = new RadioButton();
                rbYourNumQ3.GroupName = "isThisYourNum";
                rbYourNumQ3.Checked = false;
                temp = temp + 2;
                rbYourNumQ3.Text = temp.ToString() + "?";

                isThisYourNum.Controls.Add(lblYourNum);
                isThisYourNum.Controls.Add(rbYourNumQ1);
                isThisYourNum.Controls.Add(rbYourNumQ2);
                isThisYourNum.Controls.Add(rbYourNumQ3);

                /************** 
                 * Load View  *
                 **************/
                _profileView = new MultiView();
                _profileView.ID = "ProfileView";
                _profileView.Views.Add(isThisYourNum);
            }
        }
    }

    protected void btnParseInput_Click(object sender, EventArgs e)
    {
        //I don't use the userNum var? it requires me to declare an int for it to be placed in
        if (tbUserInput.Text != "")
        {
            try
            {
                Session["userNum"] = Convert.ToInt32(tbUserInput.Text);

                //Change page first. This way we don't go back to the home page from the home page...
                Session["pageOn"] = (int)Session["pageOn"] + 1;
                InitializeViews();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }

    private void InitializeViews()
    {
        int pageOn = (int)Session["pageOn"];
        //Break on the page #, call a method to set active view to that view.
        switch (pageOn)
        {
            case -1: //home page
                //Method to load home(?)
                showHome();
                break;

            case 0: //isThisYourNum?
                test();
                showQuestion();
                break;

            case 1: //GreatestCommonDenominator
                //mvFillMe.SetActiveView(GreatestCommonDenominator); Should instantiate and load in the same method
                showQuestion();
                break;
        }
    }

    private void test()
    {
        /************** 
         *  Question  *
         **************/

        int temp = (int)Session["userNum"];

        Label lblYourNum = new Label();
        lblYourNum.Text = "What number did you enter?";

        RadioButton rbYourNumQ1 = new RadioButton();
        rbYourNumQ1.GroupName = "isThisYourNum";
        rbYourNumQ1.Checked = false;
        temp--;
        rbYourNumQ1.Text = (temp).ToString() + "?";

        RadioButton rbYourNumQ2 = new RadioButton();
        rbYourNumQ2.GroupName = "isThisYourNum";
        rbYourNumQ2.Checked = false;
        rbYourNumQ2.Text = Session["userNum"].ToString() + "?";

        RadioButton rbYourNumQ3 = new RadioButton();
        rbYourNumQ3.GroupName = "isThisYourNum";
        rbYourNumQ3.Checked = false;
        temp = temp + 2;
        rbYourNumQ3.Text = temp.ToString() + "?";

        isThisYourNum.Controls.Add(lblYourNum);
        isThisYourNum.Controls.Add(rbYourNumQ1);
        isThisYourNum.Controls.Add(rbYourNumQ2);
        isThisYourNum.Controls.Add(rbYourNumQ3);

        /************** 
         * Load View  *
         **************/
        mvFillMe.Views.Add(isThisYourNum);
        Session["_ViewCount"] = (int)Session["_ViewCount"] + 1;
        mvFillMe.SetActiveView(isThisYourNum);
    }

    private void showHome()
    {
        tbUserInput.Enabled = true;
        btnParseInput.Enabled = true;
        btnBackwards.Enabled = false;
        btnForwards.Enabled = false;
    }

    private void showQuestion()
    {
        tbUserInput.Enabled = false;
        btnParseInput.Enabled = false;
        btnBackwards.Enabled = true;
        btnForwards.Enabled = true;
    }
}




Aucun commentaire:

Enregistrer un commentaire