javascript 动态Div创建asp.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15540189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic Div creation asp.net
提问by
I am trying to create Div dynamically on the press of button click.
我试图在按下按钮时动态创建 Div。
For that i refered this link>> http://forums.asp.net/t/1349244.aspx
为此,我参考了此链接>> http://forums.asp.net/t/1349244.aspx
and made code on server side(.cs page) as follows>>
并在服务器端(.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);
}
This code was giving me just one div each time when i press the button., I wanted to have addition of divs.
每次按下按钮时,这段代码只给我一个 div。我想添加 div。
On client side using javascript also i tried>>
在客户端使用 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>
Both of these didnt work.
这两个都不起作用。
What mistake am i making?
我犯了什么错误?
Is there any thing wrong?
有什么问题吗?
采纳答案by Pawan Nogariya
Use this
用这个
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);
}
回答by nunespascal
It is giving you one div, cause you are adding one div.
Remember that asp.net needs you to create all dynamically added controls on very PostBack after that.
它给了你一个 div,因为你要添加一个 div。
请记住,asp.net 需要您在此之后的 PostBack 上创建所有动态添加的控件。
If you want two controls you have to add two to the PlaceHolder.
如果您需要两个控件,则必须向 PlaceHolder 添加两个控件。
回答by MarmiK
Just use one parent div with some ID(predefined lets say id="dvDynamic"
) and runat="server"
只需使用一个带有一些 ID 的父 div(可以说是预定义的id="dvDynamic"
)和runat="server"
and then use it the dvDynamic.innerHTML = "<div> /* dynamic div contents */ </div>"
然后使用它 dvDynamic.innerHTML = "<div> /* dynamic div contents */ </div>"
Its the simplest way, as if you are using html element in ASP.net use dom controls for better generation. Dynamic creation of control will require handled, interface and many things to co-ordinate with that control. As its not predefined by system. you have to create it.
它是最简单的方法,就像您在 ASP.net 中使用 html 元素一样,使用 dom 控件进行更好的生成。控件的动态创建将需要处理、接口和许多与该控件协调的东西。因为它不是由系统预定义的。你必须创造它。
SO choose the DOM element option. that is faster and better :)
所以选择DOM元素选项。那更快更好:)
I hope this will help :)
我希望这个能帮上忙 :)