Click to See Complete Forum and Search --> : creating controls dynamically


StyM
January 29th, 2004, 02:13 AM
hello and Good Day.

may i know, how i can create controls dynamically?
i have a sample code below but just wont work.
where as in jsp this technique would work, but dont know how to do it asp.net

<form id="Form1" action="Body.aspx" method="post" runat="server">
<FONT face="MS UI Gothic"></FONT>
<% for ( int iCounter = 0 ; iCounter < 10 ; iCounter++ ) {
string strName = "employeeid" + iCounter.ToString();
%>
<asp:textbox id=<%strName;%> style="Z-INDEX: 101; LEFT: 248px; POSITION: absolute; TOP: 72px" runat="server" Width="248px" Height="24px">
</asp:textbox>
<% } %>
</form>


can somebody please help me.

thanks for the help.

ZoSoo7
January 29th, 2004, 04:26 PM
Well, I came from an ASP background similar to you and JSP. Gotta get out of the mindset of nesting the server code with-in your aspx file. I assume you have VS.NET, use the code behind always, and I mean always, it's awsome. I'd recomend doing this:

aspx page:

<form formattributes...>
<div runat=server id="myDiv">
</form>

code behind:

protected void override OnPreRender(object sender, System.EventArgs e)
{
TextBox box = new TextBox();
for ( int iCounter = 0 ; iCounter < 10 ; iCounter++ )
{
string strName = "employeeid" + iCounter.ToString();
}

box.ID = strName;
box.Styles.Add(add your styles)
this.Controls.Add(box);
base.OnPreRender(e);