本文介绍了向面板C#动态添加控件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我编写了应该将div标签动态添加到面板的代码.
第一次可以正常运行,但所有其他时间都无法正常运行.

我在代码中动态创建所有元素,因此在硬编码HTML中只有containerDiv.
这是:
HTML代码:

Hi,

I wrote code that is supposed to add div tags dynamically to a panel.
On the first time it works properly, but all the other times it doesn''t work.

I create all the elements dynamiclly in code, so I have just the containerDiv in the hard coded HTML.
Here it is:
HTML code:

<div id="insuredDeclarationsContainer" class="insuredDeclarationsContainer"  runat="server">


C#代码:
我有很多C#代码,所以我将解释我的工作:
我有mainContainer(insuredDeclarationsContainer),其中包括
一个占位符,其中包括一个面板.我需要面板,因为我需要
在divhlder的中间添加div标签.
我以这种方式添加div:


C# code:
I have a lot of c# code, so I will explain what I do:
I have the mainContainer(insuredDeclarationsContainer) which includes
a place holder, which includes a panel. I need the panel because I need
to add the div tag in the middle of the placehlder.
I add the div in the this way:

((Panel)insuredDeclarationsContainer.FindControl("columnsPanel")).Controls.Add(new LiteralControl("<div class="declarationsColumn">"));


希望你能理解这个问题.如果没有,我将尝试再次解释:
每次客户端单击按钮时,我都需要添加一个新的div.
当我第一次单击按钮时,它可以正常工作(添加了div),但在所有其他情况下,它都无法工作.如果您知道为什么会发生这种情况,请回答我.

谢谢!


I hope you understand the problem. If not I will try to explain again:
I need to add a new div every time the client clicks on a button.
When I click the button at the first time it works properly (div added), but all the other times it doesn''t work. If you have any idea why this happens, please answer me.

Thank you!

推荐答案

using System.Web.UI.HtmlControls;

HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.ID = "div1";
// add whatever attributes you need, eg make it scrollable:
div1.Attributes["style"] = "overflow: auto; width: 400px; height: 150px;";

// initialize your table
HtmlTable table1 = new HtmlTable();
table1.ID = "table1";
//....

// add the table to the div:
div1.Controls.Add(table1);

// add it to some PlaceHolder:
placeHolder1.Controls.Add(div1);


这篇关于向面板C#动态添加控件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:24