Re: How do I keep dynamically created dropdownlist when event OnSelectedIndexChanged?
- From: "Eliyahu Goldin" <removemeegoldin@xxxxxxxxxxxxxx>
- Date: Tue, 16 Aug 2005 15:47:23 +0200
Instead of creating the ddls dynamically, put them on the .aspx form in a
normal way and, in run time, control their visibility and databinding
properties.
Eliyahu
<utterberg@xxxxxxxxx> wrote in message
news:1124191626.801707.45900@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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"
> />
> <asp:PlaceHolder ID="phGroup" Runat="server" /><asp:PlaceHolder
> 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());
> }
> }
> }
> }
>
.
- Follow-Ups:
- References:
- Prev by Date: refresh display on event
- Next by Date: Re: Web User Control doesn't show up at page load
- Previous by thread: How do I keep dynamically created dropdownlist when event OnSelectedIndexChanged?
- Next by thread: Re: How do I keep dynamically created dropdownlist when event OnSelectedIndexChanged?
- Index(es):