MultiView: How to add View items programmatically

Last post 01-14-2008 3:03 AM by Amanda Wang - MSFT. 3 replies.

Sort Posts:

  • MultiView: How to add View items programmatically

    01-11-2008, 11:05 AM
    • Member
      34 point Member
    • scarabee
    • Member since 08-29-2007, 10:24 AM
    • Posts 83

    I have tried to add View items to my MultiView control, but I only got the error below. I added the my code in Page_Load event. But when I move my code to Page_PreInit I can't find my controls. I added a MultiView control in the design page. How can I reference this now. It seem unreachable from Page_PreInit. Some tips to help me on my way?

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

     Regards, Sigurd

  • Re: MultiView: How to add View items programmatically

    01-11-2008, 11:59 AM
    • Participant
      1,930 point Participant
    • moises.dl
    • Member since 09-12-2006, 3:17 PM
    • SLC
    • Posts 603

    i dont understand what youre trying to do, are you trying to add controls to your multiview view are you trying to add views to your multiview or are you trying to access either one of those?

    +
    Lvl FIVE THOUSAND DRAGON MASTER SOFTWARE ENGINEER
    my friends call me MOI
  • Re: MultiView: How to add View items programmatically

    01-12-2008, 12:13 PM
    • Member
      34 point Member
    • scarabee
    • Member since 08-29-2007, 10:24 AM
    • Posts 83

    OK, I shall try to explain better what I'm trying to accomplish.

    I'm trying to make a admin page for administrating my articles. And I want this admin page to be multilingual so I can have an English and a Norwegina version of each article. But I want the user to dynamically add or remove availabel languages for the web site, thats the reason I want to create it programmatically.

    What I want is to add View controls dynamically to my MultiView object. The number of Views I'll get from a database table containing the active languages. I tried to solve this but I got the below error. Anyone got similar code? Or can give me some tips how to solve this? Or is it better to create all controls programmatically in the code behind file?

    I only get the select box to show on the page. And when I select the other View frm the select box I get the error text below.

     

    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
     

    ------------------------------- design ----------------------------------
        <form id="form1" runat="server">
             <asp:ListBox ID="SelectViewListBox" AutoPostBack="True" Rows="1" SelectionMode="Single" OnSelectedIndexChanged="Index_Changed" runat="Server">
            </asp:ListBox>

            <hr />

            <asp:MultiView ID="MultiView1" runat="Server">
            </asp:MultiView>
        </form>

    ------------------------------- C# ----------------------------------
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                SelectViewListBox.Items.Add(new ListItem("ViewOne", "0"));
                SelectViewListBox.Items.Add(new ListItem("ViewTwo", "1"));
                SelectViewListBox.SelectedValue = "1";

                // View one
                Label lblOne = new Label();
                lblOne.Text = "View one";
                View myViewOne = new View();
                myViewOne.Controls.Add(lblOne);
                MultiView1.Views.Add(myViewOne);

                // View two
                Label lblTwo = new Label();
                lblTwo.Text = "View two";
                View myViewTwo = new View();
                myViewTwo.Controls.Add(lblTwo);
                MultiView1.Views.Add(myViewTwo);

                MultiView1.ActiveViewIndex = 0;
            }
        }

        protected void Index_Changed(Object Sender, EventArgs e) {
            MultiView1.ActiveViewIndex = int.Parse(SelectViewListBox.SelectedValue);
        }

     Regards, Sigurd
     

  • Re: MultiView: How to add View items programmatically

    01-14-2008, 3:03 AM
    Answer

    Hi,

    You can try to add the View items programmatically in the MultiView's Load event, below is my test code, it works fine on local machine:

     protected void MultiView1_Load(object sender, EventArgs e)
        {
            Label lbl = new Label();
            lbl.Text = "Hello!";

            View view = new View();
            view.Controls.Add(lbl);

            this.MultiView1.Views.Add(view);
            this.MultiView1.ActiveViewIndex = 2;
        }

    Hope it helps.

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (4 items)