如何重新创建具有On_click事件的动态生成的webcontrol

时间:2014-05-08 06:16:09

标签: c# asp.net dynamic web-controls dynamic-web-controls

当前场景: Decimal数据类型输入选择的主DDL会生成新的DDL。从新DDL输入选择后,它会生成两个标签和两个带+按钮的文本框。

当前问题:如果我单击+按钮,则会隐藏最近生成的动态Web控件。如何避免隐藏控件并在每个+按钮单击事件上连续生成它们

我的C#代码:

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)
     {
     }

1 个答案:

答案 0 :(得分:0)

没有隐藏,您必须在每个请求上重新创建动态创建的控件。

已经有一段时间了,但我认为你必须在Page_Init事件中创建动态控件才能将它们包含在视图状态中。

Some sources