How do I keep dynamically created dropdownlist when event OnSelectedIndexChanged?

Discussion in 'ASP .Net' started by utterberg@gmail.com, Aug 16, 2005.

  1. Guest

    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());
    }
    }
    }
    }
     
    , Aug 16, 2005
    #1
    1. Advertising

  2. Guest

    What I want to do is click on my "Add"-button and create two dropdowns
    - the first I fill with values from my DB. Then I select a value from
    the first drop down and fill the other drop down (with another
    roundtrip to my DB collecting the new values for the second dropdown).
    When that's done I want to click my "Add Selection"-button and add the
    selection to my webform (values from both dropdowns). After that I
    would like to add another dropdown make my selection and and add it to
    my webform, and so on.

    When I've made all my selections I would like to press a "Save"-button
    and save all the selected values to my DB.

    Thus, I do not know in advance how many selections I would like to make
    and thats why I wanna create theese controls at run time.

    Hope this clarified a bit
    ..christer
     
    , Aug 16, 2005
    #2
    1. Advertising

  3. 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

    <> wrote in message
    news:...
    > 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());
    > }
    > }
    > }
    > }
    >
     
    Eliyahu Goldin, Aug 16, 2005
    #3
  4. Do you mean that first "Add" click produces 2 ddls, then another "Add"
    produces another 2 ddls and you will have 4 ddls on the form? And 6 ddls
    after the third click?

    Can't you use the same pair of ddls for every "Add" click?

    Eliyahu

    <> wrote in message
    news:...
    > What I want to do is click on my "Add"-button and create two dropdowns
    > - the first I fill with values from my DB. Then I select a value from
    > the first drop down and fill the other drop down (with another
    > roundtrip to my DB collecting the new values for the second dropdown).
    > When that's done I want to click my "Add Selection"-button and add the
    > selection to my webform (values from both dropdowns). After that I
    > would like to add another dropdown make my selection and and add it to
    > my webform, and so on.
    >
    > When I've made all my selections I would like to press a "Save"-button
    > and save all the selected values to my DB.
    >
    > Thus, I do not know in advance how many selections I would like to make
    > and thats why I wanna create theese controls at run time.
    >
    > Hope this clarified a bit
    > .christer
    >
     
    Eliyahu Goldin, Aug 16, 2005
    #4
  5. Guest

    Ok. But how do I preserve (and view) the value when I click my "Add
    selection"-button?

    If I use a <asp:placeholder ../> for instance and try to place my drop
    down values in that place when clicking "Add selection" the page
    reloads and selection (value) is gone.

    ..christer
     
    , Aug 16, 2005
    #5
  6. Guest

    I am lousy at explaining I think. And my english ain't what it should
    be.

    I want to preserve/add values in a placeholder when I make my
    selection. I can do this with my first dd selection like this.
    - Make my selection in my drop down
    - Add selection to a placeholder by clicking "btnAddSales"

    aspx:
    <label for="selSalesAccount">Sales Account:</label>
    <asp:DropDownList Runat="server" ID="selSalesAccount"
    AutoPostBack="True" />
    <asp:Button Runat="server" ID="btnAddSales" />
    <asp:placeHolder Runat="server" ID="phSales" />

    code behind:
    private void btnAddSales_Click(object sender, EventArgs e){
    string val = this.selSalesAccount.SelectedValue;
    TextBox tb = new TextBox();
    tb.ID = val;
    this.phSales.Controls.Add(tb);
    tb.Text = val;
    tb.AutoPostBack = true;
    }

    So now I have my first drop down selection in my placeholder. Now I
    want to make another selection from my dropdown and add its value to my
    placeholder (making it two values) - but when I choose a new item in
    the drop down values preserved in my placeholder is gone.

    How do I keep thees selected values when I do another runtrip to the
    server?

    (this is a simplyfied example of what I want to do, but if I can get
    help with this I should be able to sort the rest out myself)

    ..christer
     
    , Aug 16, 2005
    #6
  7. If you have 2 static ddls, what stops you from getting their selected values
    on server side in the "Add selection" onclick event handler?

    Eliyahu

    <> wrote in message
    news:...
    > Ok. But how do I preserve (and view) the value when I click my "Add
    > selection"-button?
    >
    > If I use a <asp:placeholder ../> for instance and try to place my drop
    > down values in that place when clicking "Add selection" the page
    > reloads and selection (value) is gone.
    >
    > .christer
    >
     
    Eliyahu Goldin, Aug 16, 2005
    #7
  8. A placeholder is not good for you. First of all, choose a class you want to
    use to store the selection. It could be an array, ArrayList, SortedList,
    DataTable. Secondly, decide on how to make it persistent between the
    postbacks. You can put it in a session variable or in the ViewState. Then
    finally chose a web control that will render your selection on the page.
    Typically it would be a datagrid, a datalist or a repeater. And finally bind
    your control to your storage object on every postback.

    Eliyahu

    <> wrote in message
    news:...
    > I am lousy at explaining I think. And my english ain't what it should
    > be.
    >
    > I want to preserve/add values in a placeholder when I make my
    > selection. I can do this with my first dd selection like this.
    > - Make my selection in my drop down
    > - Add selection to a placeholder by clicking "btnAddSales"
    >
    > aspx:
    > <label for="selSalesAccount">Sales Account:</label>
    > <asp:DropDownList Runat="server" ID="selSalesAccount"
    > AutoPostBack="True" />
    > <asp:Button Runat="server" ID="btnAddSales" />
    > <asp:placeHolder Runat="server" ID="phSales" />
    >
    > code behind:
    > private void btnAddSales_Click(object sender, EventArgs e){
    > string val = this.selSalesAccount.SelectedValue;
    > TextBox tb = new TextBox();
    > tb.ID = val;
    > this.phSales.Controls.Add(tb);
    > tb.Text = val;
    > tb.AutoPostBack = true;
    > }
    >
    > So now I have my first drop down selection in my placeholder. Now I
    > want to make another selection from my dropdown and add its value to my
    > placeholder (making it two values) - but when I choose a new item in
    > the drop down values preserved in my placeholder is gone.
    >
    > How do I keep thees selected values when I do another runtrip to the
    > server?
    >
    > (this is a simplyfied example of what I want to do, but if I can get
    > help with this I should be able to sort the rest out myself)
    >
    > .christer
    >
     
    Eliyahu Goldin, Aug 16, 2005
    #8
  9. Guest

    Ok thanks!
    I'll try that.

    ..christer
     
    , Aug 16, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Reichman

    Dropdownlist onselectedIndexChanged event not fires

    Alexander Reichman, Oct 17, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    23,322
  2. Paul L
    Replies:
    1
    Views:
    7,302
  3. =?Utf-8?B?Z2xlbm4=?=

    DropDownList control and OnSelectedIndexChanged event

    =?Utf-8?B?Z2xlbm4=?=, Apr 21, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    8,358
    =?Utf-8?B?Y2xpY2tvbg==?=
    Apr 21, 2006
  4. TS
    Replies:
    2
    Views:
    1,027
  5. Paul L
    Replies:
    0
    Views:
    254
    Paul L
    May 5, 2005
Loading...

Share This Page