Create Control Dynamcially

Discussion in 'ASP .Net' started by ºa¤Ö, Nov 17, 2005.

  1. ºa¤Ö

    ºa¤Ö Guest

    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
     
    ºa¤Ö, Nov 17, 2005
    #1
    1. Advertising

  2. 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/aspnet/samples/pages/intro/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/srcview.aspx?path=~/aspnet/samples/pages/intro/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/aspnet/doc/pages/pages.aspx#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ñol : http://asp.net.do/foros/
    ======================================
    "ºa¤Ö" <ºa¤Ö@¼ö¶l.¤½¥q> wrote in message news:...
    > 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
    >
    >
     
    Juan T. Llibre, Nov 17, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quentin Huo
    Replies:
    2
    Views:
    837
    Sayed Hashimi
    Sep 8, 2004
  2. JJ
    Replies:
    1
    Views:
    469
    Munna
    Jun 16, 2008
  3. Christopher

    dynamcially created data structure

    Christopher, Oct 9, 2008, in forum: C++
    Replies:
    2
    Views:
    335
    Pascal J. Bourguignon
    Oct 10, 2008
  4. wpy

    how to create sub control within sub control?

    wpy, Apr 7, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    164
    Stefan
    Apr 8, 2004
  5. Jonathan Eric Miller
    Replies:
    2
    Views:
    358
    Jonathan Eric Miller
    Jul 22, 2004
Loading...

Share This Page