ASP.NET Wizard control with Dynamic UserControls
Posted
by wjat777
on Stack Overflow
See other posts from Stack Overflow
or by wjat777
Published on 2010-03-23T12:50:11Z
Indexed on
2010/03/23
12:53 UTC
Read the original article
Hit count: 785
Hello everyone
I'm trying to use the Wizard control as follows: 1 - In the first step (first screen) has a cheklistbox with several options 2 - For each option selected, will be created an extra step 3 - In some steps may be set up intermediate stages
Me problem is that each step is a usercontrol. So if the first step I select option1 and option3, I should create two more steps with usercontrol1 and UserControl3 ....
Someone already did something similar?
I will try explain better the problem.
I'll paste here, but I put a copy of the project in skydrive: http://cid-3d949f1661d00819.skydrive.live.com/self.aspx/C%5E3/WizardControl2.7z
It is a very basic example of what I'm trying to do. The project has page.ASPX 1 and 3 usercontrol.ASCX (UC1, UC2 and UC3)
Default.ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
Option1
Option2
Option3
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreInit(object sender, EventArgs e)
{
LoadSteps();
}
private void LoadSteps()
{
int count = Wizard1.WizardSteps.Count;
for (int i = count - 1; i > 0; i--)
{
WizardStepBase step = Wizard1.WizardSteps[i];
if (step.StepType != WizardStepType.Start)
Wizard1.WizardSteps.Remove(step);
}
string Activities="";
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
WizardStep step = new WizardStep {ID = "step_" + item.Value, Title = "step_" + item.Value};
UserControl uc=null;
switch (item.Value)
{
case "1":
uc=(UserControl)LoadControl("~/UC1.ascx");
break;
case "2":
uc=(UserControl)LoadControl("~/UC2.ascx");
break;
case "3":
uc=(UserControl)LoadControl("~/UC3.ascx");
break;
}
step.Controls.Add(uc);
Wizard1.WizardSteps.Add(step);
}
}
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
LoadSteps();
}
}
}
Control UC1.ASCX to UC3.ASCX has the same code (as an example)
usercontrol.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UC1.ascx.cs" Inherits="WebApplication2.UC1" %>
AutoPostBack="True">
AutoPostBack="True">
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class UC1 : System.Web.UI.UserControl
{
protected void Page_Init(object sender, EventArgs e)
{
List list1 = new List { "Alice", "Bob", "Chris" };
List list2 = new List { "UN", "DEUX", "TROIS" };
DropDownList1.DataSource = list1;
DropDownList2.DataSource = list2;
DropDownList1.DataBind();
DropDownList2.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedValue;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Label2.Text = DropDownList2.SelectedValue;
}
}
}
You can see that the controls UserControls trigger events (they cause postbacks)
The behavior of this project is a little different than I described in the first thread, but the problem is the same. After the second time "next" button, you will get an error.
Thanks in advance
William
© Stack Overflow or respective owner