I have written small example which you need to modify to suit your needs but it demonstrates one way to deal with dynamical controls on Page.
private void Page_Load(object sender, System.EventArgs e)
{
if(ShouldCreateUsaDDL)
{
CreateUSA();
}
if(ShouldCreateBrazilDDL)
{
CreateBrazil();
}
}
//Button click handler to indicate we want to create a DDL for USA
private void Button1_Click(object sender, System.EventArgs e)
{
if(!ShouldCreateUsaDDL)
{
CreateUSA();
ShouldCreateUsaDDL=true;
ShouldCreateBrazilDDL = false;
}
}
public bool ShouldCreateUsaDDL
{
get
{
object obj=ViewState["ShouldCreateUsaDDL"];
return (obj==null)? false : (bool)obj;
}
set
{
ViewState["ShouldCreateUsaDDL"]=value;
}
}
public bool ShouldCreateBrazilDDL
{
get
{
object obj=ViewState["ShouldCreateBrazilDDL"];
return (obj==null)? false : (bool)obj;
}
set
{
ViewState["ShouldCreateBrazilDDL"]=value;
}
}
private void CreateUSA()
{
DropDownList list=new DropDownList();
list.ID="USA";
list.Items.Add("Tallahassee");
list.Items.Add("Tennessee");
list.Items.Add("Washington");
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(list);
}
private void CreateBrazil()
{
DropDownList list=new DropDownList();
list.ID="Brazil";
list.Items.Add("Brazil1");
list.Items.Add("Brazil2");
list.Items.Add("Brazil3");
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(list);
}
private void Button3_Click(object sender, System.EventArgs e)
{
//Button click handler to indicate we want to create a DDL for Brazil
if(!ShouldCreateBrazilDDL)
{
CreateBrazil();
ShouldCreateUsaDDL=false;
ShouldCreateBrazilDDL = true;
}
}
Note that it assumes you to have two buttons on Page (and a PlaceHolder) with which you control what you want to be visible (which DDL).
Thanks,
Teemu Keiski
Finland, EU