Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Create Control Dynamcially

Reply
Thread Tools

Create Control Dynamcially

 
 
榮少
Guest
Posts: n/a
 
      11-17-2005
In tradition ASP, i can create a template like follow

<%
for i = 1 to 3
%>
<table>
<tr>
<td>Name: </td>
<td><input type="text" name="textName<%=i%>"/></td>
</tr>
</table>
<%
next
%>

But how to do it in ASP.net? I have tried using like this

<%
for (int i = 1; i < 3; i++) {%>
<table>
<tr>
<td>Name: </td>
<td><asp:TextBox id="textName<%#i%>"/></td>
</tr>
</table>
<%
} %>

but it not works....thanks


 
Reply With Quote
 
 
 
 
Juan T. Llibre
Guest
Posts: n/a
 
      11-17-2005
You were very close. This works, sort of :

<table>
<% for (int i = 1; i < 3; i++) { %>
<tr>
<td>Name: </td>
<td><asp:TextBox id="textName<%=i%>"/></td>
</tr>
<% } %>
</table>

If you view source, you'll see "textname1" and "textname2",
but that isn't very useful, is it ?

I say "sort of" because you can use this to repeat blocks of text,
with different font sizes, like the example in the QuickStart:
http://beta.asp.net/QUICKSTART/aspne...intro2_cs.aspx
but you cannot create controls with this type of code, because
as soon as you insert a second set of quotes into the line, i.e.,

<td><asp:TextBox id="textName<%=i%>" runat="server"/></td>

....the compiler will throw an exception.

So, you can use that type of code, to repeat any text,
IF you use only one set of quotes inside your brackets.

While <% %> code blocks provide a powerful way to custom manipulate
the text output returned from an ASP.NET page, they do not provide a
clean HTML programming model.

See :
http://beta.asp.net/QUICKSTART/util/...tro/intro3.src

As the sample above illustrates, developers using only <% %> code blocks
must custom manage page state between round trips and custom interpret posted values.

<%
This recommendation is from :
http://beta.asp.net/QUICKSTART/aspne...x#writingpages
%>

See intro2.aspx and intro3.aspx



Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Espa隳l : http://asp.net.do/foros/
======================================
"榮少" <榮少@熱郵.公司> wrote in message news:(E-Mail Removed)...
> In tradition ASP, i can create a template like follow
>
> <%
> for i = 1 to 3
> %>
> <table>
> <tr>
> <td>Name: </td>
> <td><input type="text" name="textName<%=i%>"/></td>
> </tr>
> </table>
> <%
> next
> %>
>
> But how to do it in ASP.net? I have tried using like this
>
> <%
> for (int i = 1; i < 3; i++) {%>
> <table>
> <tr>
> <td>Name: </td>
> <td><asp:TextBox id="textName<%#i%>"/></td>
> </tr>
> </table>
> <%
> } %>
>
> but it not works....thanks
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamcially created data structure Christopher C++ 2 10-10-2008 12:04 PM
Easiest way to create a Server Control from a Web User Control JJ ASP .Net 1 06-16-2008 12:20 PM
How to dynamically create a control in a user control? Quentin Huo ASP .Net 2 09-08-2004 12:23 PM
Possible to create a composite control that has a child control that is a validator that validates the composite control itself? Jonathan Eric Miller ASP .Net Building Controls 2 07-22-2004 10:58 PM
how to create sub control within sub control? wpy ASP .Net Building Controls 1 04-07-2004 11:06 PM



Advertisments