Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# 如何在.net中动态添加控件?_C#_Asp.net_User Controls - Fatal编程技术网

C# 如何在.net中动态添加控件?

C# 如何在.net中动态添加控件?,c#,asp.net,user-controls,C#,Asp.net,User Controls,我想动态添加控件 代码: .aspx 签证号码: 国家名称: 签证类型: 条目类型: 到期日 这里我使用的是用户控件 <asp:Repeater ID="rpt1" runat="server"> <ItemTemplate> user control code goes here but first put it in a div(example id - div1) with `style="display:none"` </ItemTemplate&g

我想动态添加控件

代码:

.aspx


签证号码:
国家名称:
签证类型:
条目类型:
到期日

这里我使用的是用户控件

<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
 user control code goes here but first put it in a div(example id - div1) with `style="display:none"`
</ItemTemplate>
</asp:Repeater>
.ascx


签证号码:
国家名称:
签证类型:
条目类型:
到期日
.aspx.cs

如果单击“添加”按钮,一行添加成功,但当我再次关闭和打开时,以前添加的行也会出现问题

我认为问题是静态int I=0

还有别的办法吗

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

        }
    }

    static int i = 0;
    protected void addnewtext_Click(object sender, EventArgs e)
    {
        i++;
        for (int j = 0; j <= i; j++)
        {
            AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
            PlaceHolder1.Controls.Add(ac);
            PlaceHolder1.Controls.Add(new LiteralControl("<BR>"));
        }
    }
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
}
}
静态int i=0;
受保护的void addnewtext\u单击(对象发送者,事件参数e)
{
i++;

对于(int j=0;j您是否可以使用repeater/gridview控件代替占位符控件,我认为这是适合您的位置的最佳方法

我推荐此解决方案。它与您正在使用的代码不同,但仍然非常简单。 您可以使用中继器控件

<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
 user control code goes here but first put it in a div(example id - div1) with `style="display:none"`
</ItemTemplate>
</asp:Repeater>
稍后在页面上的javascript中,您可以找到id为div1的第一个div,该div具有style-display:none并显示它。这样,您将显示下一个带有字段的“行”

编辑1

如果您可以在单个
中添加用户控件中的所有字段,则不需要style=display:none的div。您只需将该class=“GridBody”添加到
-例如:

然后对行执行以下操作:

<tr class="GridRow" id="row1" style="display: none">
...
</tr>

我认为你是对的。这是因为变量I的静态声明。任何使它静态的具体原因。如果我们使用静态,行数将增加
rpt1.DataSource = Utils.GetRptTable(4); // will add the code 4 times but it will not be   visible
rpt1.DataBind();
<tr class="GridRow" id="row1" style="display: none">
...
</tr>
 function addRow() {
        var body = $(".GridBody");
        var indexOfNextRow = $('tr[class="GridRow"][hidden="false"]').length;
        var tr = $(".GridBody tr")[indexOfNextRow];
        tr.style.display = 'table-row';
        tr.setAttribute('hidden', 'false');
    }