U
utterberg
Hi
I'm adding two dropdownlist in my webform by clicking a button. I fill
the first dropdown with some values from my DB and the other one when I
choose from the first dropdown. But since theese dropdownlists are
created by clicking a button they no longer exists on my webform. How
do I keep my dynamically created dropdown when using
OnSelectedIndexChanged? Can anyone help me solve this problem? Or is
there a better way of doing this?
Thanks
..christer
(here's some of my code)
aspx-page:
<asp:Button Runat="server" ID="btnAddGroup" Text="Add Main-/Subgroup"
/>
<asplaceHolder ID="phGroup" Runat="server" /><asplaceHolder
ID="phSubGroup" Runat="server" />
code behinde (aspx.cs):
private void btnAddGroup_Click(object sender, System.EventArgs e){
CreateDropDown(this.phGroup, "selMainGrp");
}
private void CreateDropDown(PlaceHolder ph, string id){
DropDownList dd = new DropDownList();
FillMainGrpListBox(ph, dd, id);
}
private void FillMainGrpListBox(PlaceHolder ph, DropDownList dd, string
id){
DropDownList selMainGroup = dd;
string sqlProc = "GetMainGroup";
Data data = new Data();
resultDataSet = data.GetDataSetByProcName(sqlProc);
dd.Items.Add("Choose");
//Fill control
foreach(DataTable table in resultDataSet.Tables){
foreach(DataRow row in table.Rows){
foreach(DataColumn col in table.Columns){
//Add maingroup to dropdownlist
selMainGroup.Items.Add(row[col].ToString());
}
}
}
//Add control
string sAttributes;
sAttributes = "<b>Main Group</b>";
ph.Controls.Add(new LiteralControl(sAttributes));
selMainGroup.ID = id;
selMainGroup.AutoPostBack = true;
selMainGroup.SelectedIndexChanged +=new
EventHandler(selMainGroup_SelectedIndexChanged);
ph.Controls.Add(selMainGroup);
//Add Subgroup dropdown
DropDownList ddSub = new DropDownList();
sAttributes = "<b>Sub group</b>";
ddSub.ID = "selSubGrp";
ddSub.Items.Add("Choose");
phSubGroup.Controls.Add(new LiteralControl(sAttributes));
phSubGroup.Controls.Add(ddSub);
}
private void selMainGroup_SelectedIndexChanged(object sender,
System.EventArgs e){
string mainGroupValue = this.selMainGrp.SelectedValue;
if(mainGroupValue != "Choose"){
//Reset subgroup dropdown
SetDefaultListBoxValue(selSubGrp);
//Add values in subgroup
FillSubGrpListBox(mainGroupValue);
}
}
private void FillSubGrpListBox(string mainGroupValue){
string sqlProc = "GetSubGroup";
string[,] paramArr = {{"@MainGroup",mainGroupValue}};
Data data = new Data();
resultDataSet = data.GetDataSetByProcName(sqlProc, paramArr);
foreach(DataTable table in resultDataSet.Tables){
foreach(DataRow row in table.Rows){
foreach(DataColumn col in table.Columns){
selSubGrp.Items.Add(row[col].ToString());
}
}
}
}
I'm adding two dropdownlist in my webform by clicking a button. I fill
the first dropdown with some values from my DB and the other one when I
choose from the first dropdown. But since theese dropdownlists are
created by clicking a button they no longer exists on my webform. How
do I keep my dynamically created dropdown when using
OnSelectedIndexChanged? Can anyone help me solve this problem? Or is
there a better way of doing this?
Thanks
..christer
(here's some of my code)
aspx-page:
<asp:Button Runat="server" ID="btnAddGroup" Text="Add Main-/Subgroup"
/>
<asplaceHolder ID="phGroup" Runat="server" /><asplaceHolder
ID="phSubGroup" Runat="server" />
code behinde (aspx.cs):
private void btnAddGroup_Click(object sender, System.EventArgs e){
CreateDropDown(this.phGroup, "selMainGrp");
}
private void CreateDropDown(PlaceHolder ph, string id){
DropDownList dd = new DropDownList();
FillMainGrpListBox(ph, dd, id);
}
private void FillMainGrpListBox(PlaceHolder ph, DropDownList dd, string
id){
DropDownList selMainGroup = dd;
string sqlProc = "GetMainGroup";
Data data = new Data();
resultDataSet = data.GetDataSetByProcName(sqlProc);
dd.Items.Add("Choose");
//Fill control
foreach(DataTable table in resultDataSet.Tables){
foreach(DataRow row in table.Rows){
foreach(DataColumn col in table.Columns){
//Add maingroup to dropdownlist
selMainGroup.Items.Add(row[col].ToString());
}
}
}
//Add control
string sAttributes;
sAttributes = "<b>Main Group</b>";
ph.Controls.Add(new LiteralControl(sAttributes));
selMainGroup.ID = id;
selMainGroup.AutoPostBack = true;
selMainGroup.SelectedIndexChanged +=new
EventHandler(selMainGroup_SelectedIndexChanged);
ph.Controls.Add(selMainGroup);
//Add Subgroup dropdown
DropDownList ddSub = new DropDownList();
sAttributes = "<b>Sub group</b>";
ddSub.ID = "selSubGrp";
ddSub.Items.Add("Choose");
phSubGroup.Controls.Add(new LiteralControl(sAttributes));
phSubGroup.Controls.Add(ddSub);
}
private void selMainGroup_SelectedIndexChanged(object sender,
System.EventArgs e){
string mainGroupValue = this.selMainGrp.SelectedValue;
if(mainGroupValue != "Choose"){
//Reset subgroup dropdown
SetDefaultListBoxValue(selSubGrp);
//Add values in subgroup
FillSubGrpListBox(mainGroupValue);
}
}
private void FillSubGrpListBox(string mainGroupValue){
string sqlProc = "GetSubGroup";
string[,] paramArr = {{"@MainGroup",mainGroupValue}};
Data data = new Data();
resultDataSet = data.GetDataSetByProcName(sqlProc, paramArr);
foreach(DataTable table in resultDataSet.Tables){
foreach(DataRow row in table.Rows){
foreach(DataColumn col in table.Columns){
selSubGrp.Items.Add(row[col].ToString());
}
}
}
}