Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态添加的控件未显示在用户控件中_C#_Asp.net - Fatal编程技术网

C# 动态添加的控件未显示在用户控件中

C# 动态添加的控件未显示在用户控件中,c#,asp.net,C#,Asp.net,当用户单击页面上的Add按钮时,我需要重复一些字段集(文本框)web表单UI。最初,他们看到一个控件只有几个字段,当他们单击“添加”时,另一组相同的字段将添加到页面中。 因此,我对重复UI进行了用户控制,当用户单击页面上的Add按钮时,我尝试动态生成文本框并将其添加到页面中。 但由于某些原因,我动态生成的UI不可见。我相信这可能是一个观点国家的问题。但不知道如何解决 下面是我试图在用户控件的按钮单击方法上添加到Usercontrol的更新面板的代码 protected void AddBtn_C

当用户单击页面上的
Add
按钮时,我需要重复一些字段集(文本框)web表单UI。最初,他们看到一个控件只有几个字段,当他们单击“添加”时,另一组相同的字段将添加到页面中。 因此,我对重复UI进行了用户控制,当用户单击页面上的Add按钮时,我尝试动态生成文本框并将其添加到页面中。 但由于某些原因,我动态生成的UI不可见。我相信这可能是一个观点国家的问题。但不知道如何解决

下面是我试图在用户控件的按钮单击方法上添加到Usercontrol的更新面板的代码

protected void AddBtn_Click(object sender, EventArgs e)
    {
        Panel panel = AddPanel(this.NumberOfDynamicControls);
        AddVRMOrSink(panel, this.NumberofTextBoxes, this.NumberOfDynamicControls);

        this.NumberOfDynamicControls++; // update number of controls   
    }

private void AddVRMOrSink(Panel panel, int textboxCounter, int i)
    {
        string[] labels = this.DisplayNames.Split('_');

        TableRow tRow1 = new TableRow();
        TableRow tRow2 = new TableRow();
        TableRow tRow3 = new TableRow();
        TableRow tRow4 = new TableRow();
        tRow1.Width = new Unit(350, UnitType.Pixel);
        tRow2.Width = new Unit(350, UnitType.Pixel);
        tRow3.Width = new Unit(350, UnitType.Pixel);
        tRow4.Width = new Unit(350, UnitType.Pixel);

        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();
        TableCell cell21 = new TableCell();
        TableCell cell22 = new TableCell();
        TableCell cell31 = new TableCell();
        TableCell cell32 = new TableCell();
        TableCell cell41 = new TableCell();
        TableCell cell42 = new TableCell();

        tRow1.Cells.Add(cell1);
        tRow1.Cells.Add(cell2);

        tRow2.Cells.Add(cell21);
        tRow2.Cells.Add(cell22);

        tRow3.Cells.Add(cell31);
        tRow3.Cells.Add(cell32);

        tRow4.Cells.Add(cell41);
        tRow4.Cells.Add(cell42);

        TextBox txt_1 = AddTextBox(textboxCounter++, true);
        TextBox txt_2 = AddTextBox(textboxCounter++);
        TextBox txt_3 = AddTextBox(textboxCounter++);
        TextBox txt_4 = AddTextBox(textboxCounter++);
        TextBox txt_5 = AddTextBox(textboxCounter++);

        Label lbl_1 = AddLabel(labels[0]);
        Label lbl_2 = AddLabel(labels[1]);
        Label lbl_3 = AddLabel(labels[2]);
        Label lbl_4 = AddLabel(labels[3]);
        Label lbl_5 = AddLabel(labels[4]);


        Button deleteButton = AddDeleteButton(i + 1);

        cell1.Controls.Add(lbl_1);
        cell1.Controls.Add(txt_1);

        cell2.Controls.Add(lbl_2);
        cell2.Controls.Add(txt_2);

        cell22.Controls.Add(lbl_3);
        cell22.Controls.Add(txt_3);

        cell32.Controls.Add(lbl_4);
        cell32.Controls.Add(txt_4);

        cell42.Controls.Add(lbl_5);
        cell42.Controls.Add(txt_5);

        panel.Controls.Add(tRow1);
        panel.Controls.Add(tRow2);
        panel.Controls.Add(tRow3);
        panel.Controls.Add(tRow4);

        panel.Controls.Add(deleteButton);

        this.ControlsTable.Rows.Add(tRow1);
        this.ControlsTable.Rows.Add(tRow2);
        this.ControlsTable.Rows.Add(tRow3);
        this.ControlsTable.Rows.Add(tRow4);

        if (!isVRM)
        {
            Label lbl_6 = AddLabel(labels[5]);
            TextBox txt_6 = AddTextBox(textboxCounter++);
            TableRow tRow5 = new TableRow();
            tRow5.Width = new Unit(350, UnitType.Pixel);
            TableCell cell51 = new TableCell();
            TableCell cell52 = new TableCell();
            TableCell cell53 = new TableCell();

            cell52.Controls.Add(lbl_6);
            cell52.Controls.Add(txt_6);
            cell53.Controls.Add(deleteButton);

            tRow5.Cells.Add(cell51);
            tRow5.Cells.Add(cell52);
            tRow5.Cells.Add(cell53);

            panel.Controls.Add(tRow5);
            this.ControlsTable.Rows.Add(tRow5);
        }
        else
        {
            TableCell cell43 = new TableCell();
            cell43.Controls.Add(deleteButton);
            tRow4.Cells.Add(cell43);
        }
        UpdatePanel1.ContentTemplateContainer.Controls.Add(panel);
        DynamicControlPlaceHolder.Controls.Add(panel);
    }
usercontrol.ascx是



您可以第一次看到吗?是的,但在随后单击“添加”按钮时无法看到。不确定原因…:(动态控件不保持其状态,您需要在预整型阶段预填充它们..因此..您能给我提供更多详细信息…或一个示例或w链接供我查看和理解吗?但是为什么第一次单击添加成功,而其余的没有成功?