DropDownList only appearing once

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stoogots2
    New Member
    • Sep 2007
    • 77

    DropDownList only appearing once

    I guess my problem begs the question, how do I store a DropDownList (or other control in Application State and pull it out and reuse it? In my web application Global.asax.cs I am creating a DropDownList and throwing it into Application state, hoping to reuse it on this page (and wherever else). This code should dynamically create a table with 3 rows and a ddl0 for each. It only creates one DDL and displays it in the 3rd row, very strange.

    Code:
                protected override void OnInit(EventArgs e)
                {
                    sSearchConditions = new string[] { "", "Begins With", "Contains", "Does Not Contain", "Ends With", "Equals", "Not Equal" };
                    base.OnInit(e);
                    Table tSearch = new Table();
                    tSearch.ID = "tCriteria";
                    TableHeaderRow thr = new TableHeaderRow();
    
                    for (int n = 0; n < 3; n++)
                    {
                        Label lblFields = new Label();
                        lblFields.Text = "Select Search Field:";
                        lblFields.CssClass = "tLBLText";
                        Label lblCondition = new Label();
                        lblCondition.Text = "Select Condition: ";
                        lblCondition.CssClass = "tLBLText";
                        Label lblValue = new Label();
                        lblValue.Text = "Type Value: ";
                        lblValue.CssClass = "tLBLText";
    
                        //Fields
                        DropDownList ddl0 = new DropDownList();
                        ddl0 = (DropDownList)Application[Global.DDLFields];
                        ddl0.ID = "ddlFields" + n.ToString();
                        //Conditions
                        DropDownList ddl1 = new DropDownList();
                        ddl1.ID = "ddlCondition" + n.ToString();
                        ddl1.DataSource = sSearchConditions;
                        ddl1.DataBind();
    
                        TextBox tb1 = new TextBox();
                        tb1.ID = "tb" + n.ToString();
                        tb1.CssClass = "tcText";
                        TableRow tr = new TableRow();
    
                        TableCell tc1 = new TableCell();
                        tc1.Controls.Add(lblFields);
                        tc1.Controls.Add(ddl0);
    
                        TableCell tc2 = new TableCell();
                        tc2.Controls.Add(lblCondition);
                        tc2.Controls.Add(ddl1);
    
                        TableCell tc3 = new TableCell();
                        tc3.Controls.Add(lblValue);
                        tc3.Controls.Add(tb1);
                        tr.Cells.Add(tc1);
                        tr.Cells.Add(tc2);
                        tr.Cells.Add(tc3);
                        tSearch.Rows.Add(tr);
                    }
                    pCustomSearch.Controls.Add(tSearch);
                    //pCustomSearch is a Panel Control
  • balame2004
    New Member
    • Mar 2008
    • 142

    #2
    Code you have given works perfectly. Could you please give me code to define DDLFields so I can able to reproduce the issue.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by stoogots2
      I guess my problem begs the question, how do I store a DropDownList (or other control in Application State and pull it out and reuse it? In my web application Global.asax.cs I am creating a DropDownList and throwing it into Application state, hoping to reuse it on this page (and wherever else). This code should dynamically create a table with 3 rows and a ddl0 for each. It only creates one DDL and displays it in the 3rd row, very strange.
      You shouldn't be storing the a page level control in the Application State.

      The Global.asax is used to mange application level objects like Session (not the objects within Session but Session itself...it handles the Events that Session raises like Session_Start and Session_End).

      Controls like DropDownLists are page level controls, not application level controls. They exist on a page and the page code manages them. The page listen for, and handles events that these controls raise...

      Instead of trying to store the DropDownList like you are, you should consider using the Global.asax to create/destroy a cached version data source that you can use with the page level controls.

      Or you could create a UserControl that can be used on any page that you require.

      What exactly are you trying to do before I start recommending 100 different ways to solve your problem?

      Comment

      • stoogots2
        New Member
        • Sep 2007
        • 77

        #4
        I switched things around last week, but I was trying to reuse a DropDownList as it appears in several places throughout the application. I realize that I could've utilized the Global.asax.cs (which I actually use) to store the datasource, but I thought it would be "niftier" if I could bind the data to the DropDownList and store, and reuse that whenever I need it (and not having to call DataBind). If you guys know a good way to do this, and have the time, I would love to learn.

        Thanks,

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Is it just the DropDownList or are there other controls that go with the DropDownList to make something work?

          Check out user controls :)

          Comment

          Working...