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