Question

Current scenario: Main DDL of Decimal datatype input selection generates a new DDL. Upon input selection from new DDL it generates two labels and two textboxes with + button.

Current Issue: If i click the + button the recently generated dynamic web controls are hiding. How to avoid the hiding of controls and generate them continuously on every + button click event

My C# Code:

protected void Page_Load(object sender, EventArgs e)
    {                      
        if (!IsPostBack)
        {
            BindDropDownLists();
        }

        else
        {
            if (!String.IsNullOrEmpty(DropDownList5.SelectedValue))
            {
                if (DropDownList5.SelectedValue == "decimal")
                {
                    createdynamiccontrols_decimal();
                }                                                        
            }

        }
    }

protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
     {

     }

    protected void createdynamiccontrols_decimal()
     {
         int i = DropDownList5.SelectedIndex;
         ++i;
         TableRow row = new TableRow();
         row.ID = "TableRow_" + i.ToString();
         TableCell cell1 = new TableCell();

         DropDownList Range_DDL_Decimal = new DropDownList();
         Range_DDL_Decimal.ID = "RandeDDL_Decimal" + i.ToString();
         Range_DDL_Decimal.Items.Insert(0, new ListItem("--Select--", "--Select--"));
         Range_DDL_Decimal.Items.Insert(1, new ListItem("Equal", "Equal"));
         Range_DDL_Decimal.Items.Insert(2, new ListItem("NotEqual", "NotEqual"));
         Range_DDL_Decimal.Items.Insert(3, new ListItem("greater than", "greater than"));
         Range_DDL_Decimal.Items.Insert(4, new ListItem("lesser than", "lesser than"));
         Range_DDL_Decimal.Items.Insert(5, new ListItem("greater than or equal to", "greater than or equal to"));
         Range_DDL_Decimal.Items.Insert(6, new ListItem("lesser than or equal to", "lesser than or equal to"));
         Range_DDL_Decimal.Items.Insert(7, new ListItem("Contains", "Contains"));
         Range_DDL_Decimal.Items.Insert(8, new ListItem("Is Null", "Is Null"));
         Range_DDL_Decimal.Items.Insert(9, new ListItem("Is Not Null", "Is Not Null"));
         Range_DDL_Decimal.Items.Insert(10, new ListItem("Between", "Between"));

         Range_DDL_Decimal.AutoPostBack = true;
         Range_DDL_Decimal.SelectedIndexChanged += new System.EventHandler(Range_DDL_Decimal_SelectedIndexChanged);

         cell1.Controls.Add(Range_DDL_Decimal);

         //// Add the TableCell to the TableRow  
         row.Cells.Add(cell1);

          dynamic_filter_table.Rows.Add(row);
          dynamic_filter_table.EnableViewState = true;
          ViewState["dynamic_filter_table"] = true;

     }

 protected void Range_DDL_Decimal_SelectedIndexChanged(object sender, EventArgs e)
     {
         int j = DropDownList5.SelectedIndex;
         ++j;

         TableRow rowtwo;
         rowtwo = new TableRow();

         TableCell cell1 = new TableCell();
         TableCell cell2 = new TableCell();
         TableCell cell3 = new TableCell();

         TextBox tb1 = new TextBox();
         TextBox tb2 = new TextBox();

         Label lbl1 = new Label();
         Label lbl2 = new Label();

         // Set a unique ID for each TextBox added      
         tb1.ID = "lowerbound_" + j.ToString();
         tb2.ID = "upperbound_" + j.ToString();
         lbl1.Text = "LowerBound:";
         lbl1.Font.Size = FontUnit.Point(10);
         lbl1.Font.Bold = true;
         lbl1.Font.Name = "Arial";

         lbl2.Text = "UpperBound:";
         lbl2.Font.Size = FontUnit.Point(10);

         lbl2.Font.Bold = true;
         lbl2.Font.Name = "Arial";

         Button addmore = new Button();
         addmore.ID = "Button_Decimal" + j.ToString();
         addmore.Text = "+";
         addmore.Click += new System.EventHandler(Addmore_click);
         **//If this button is clicked the Dynamic controls are vanished**
         cell1.Controls.Add(lbl1);
         cell1.Controls.Add(tb1);
         cell2.Controls.Add(lbl2);
         cell2.Controls.Add(tb2);

         cell3.Controls.Add(addmore);


         rowtwo.Cells.Add(cell1);
         rowtwo.Cells.Add(cell2);
         rowtwo.Cells.Add(cell3);

         dynamic_filter_table.Rows.Add(rowtwo);

         dynamic_filter_table.EnableViewState = true;
         ViewState["dynamic_filter_table"] = true;

     }

protected void Addmore_click(object sender, EventArgs e)
     {
       **//Generate dynamic controls on every click of + button 
       //How to procced**
     }

 protected void Page_PreInit(object sender, EventArgs e)
     {
     }
Était-ce utile?

La solution

There are not hiding, you have to recreate the dynamically created controls on every request.

It has been a while, but I think you have to create the dynamic controls in the Page_Init event for them to be included in the view-state.

Some sources

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top