gpt4 book ai didi

c# - 动态 Div 创建 asp.net

转载 作者:搜寻专家 更新时间:2023-11-01 04:26:17 25 4
gpt4 key购买 nike

我试图在按下按钮时动态创建 Div。

为此我引用了这个链接>> http://forums.asp.net/t/1349244.aspx

并在服务器端(.cs页面)编写代码如下>>

public static int i = 0;
protected void Button1_Click(object sender, EventArgs e)
{
i++;
HtmlGenericControl newControl = new HtmlGenericControl("div");

newControl.ID = "NEWControl"+i;
newControl.InnerHtml = "This is a dynamically created HTML server control.";

PlaceHolder1.Controls.Add(newControl);
}

当我每次按下按钮时,这段代码只给我一个 div。我想添加 div。

我也尝试在客户端使用 javascript>>

<body>
<form id="form1" runat="server">
<div>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick="addDiv();" />

</div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</form>
</body>
</html>
<script type="text/javascript">
function addDiv() {
alert("Control comming in function");
var r = document.createElement('Div');
r.style.height = "20px";
r.style.width = "25px";
r.appendChild("div");
alert("Control going out of function");
}
</script>

这两个都不起作用。

我犯了什么错误?

有什么问题吗?

最佳答案

使用这个

    public int Index
{
get
{
if(ViewState["Index"]==null)
{
ViewState["Index"]=0;
}
else
{
ViewState["Index"]=int.Parse(ViewState["Index"].ToString())+1;
}

return int.Parse(ViewState["Index"].ToString());
}
}

protected void Button1_Click(object sender, EventArgs e)
{
HtmlGenericControl newControl = new HtmlGenericControl("div");
newControl.ID = "NEWControl"+Index;
newControl.InnerHtml = "This is a dynamically created HTML server control.";

PlaceHolder1.Controls.Add(newControl);
}

关于c# - 动态 Div 创建 asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15540189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com