Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  146] [ 7]  / answers: 1 / hits: 15308  / 11 Years ago, thu, march 21, 2013, 12:00:00

I am trying to create Div dynamically on the press of button click.



For that i refered this link>> http://forums.asp.net/t/1349244.aspx



and made code on server side(.cs page) as follows>>



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.



On client side using javascript also i tried>>



<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?


More From » c#

 Answers
64

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);
}

[#79445] Wednesday, March 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shekinah

Total Points: 699
Total Questions: 112
Total Answers: 110

Location: Philippines
Member since Sat, Jul 11, 2020
4 Years ago
;