create dropdownlist in runtime with event to create another dropdown.

Last post 09-11-2004 3:39 PM by llangleyben. 2 replies.

Sort Posts:

  • create dropdownlist in runtime with event to create another dropdown.

    09-08-2004, 5:00 AM
    • Member
      10 point Member
    • nu_ange
    • Member since 09-08-2004, 4:16 AM
    • Posts 2
    I have a dropdownlist which will call class to add another dropdownlist into placeholder of the page as SelectIndexChange event raised.
    The problem is
    I want the new dropdownlist to be able to add another dropdownlist on SelectIndexChange event raised too and the new one should have this event as well. Then when user select dropdownlist it will always create a new one.

    Anybody got an idea ??
  • Re: create dropdownlist in runtime with event to create another dropdown.

    09-11-2004, 2:49 AM
    • All-Star
      46,040 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    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
  • Re: create dropdownlist in runtime with event to create another dropdown.

    09-11-2004, 3:39 PM
    • Star
      8,315 point Star
    • llangleyben
    • Member since 06-25-2004, 12:30 PM
    • Israel
    • Posts 1,660
    Read this article.
    Leon Langleyben

    MCSD, ASP.NET MVP

    Blog
Page 1 of 1 (3 items)