input control id and for loop

Discussion in 'ASP .Net' started by Neil Zanella, Jan 30, 2005.

  1. Neil Zanella

    Neil Zanella Guest

    Hello,

    I have a bunch of HTML input controls which I need to generate from
    within a for loop. I would like them to have id attributes set to
    values foo1, foo2, foo3, foo4, etc...

    Unfortunately it seems this is not possible because ASP.NET won't
    let me use ASP tags within the values of the id attribute. So
    for instance I cannot just do the following:

    <% for (i = 0; i < N; i++) { %>
    <input id="foo<%= i %>" type="text" runat="server" />
    <% } %>

    The reason is that IIS will issue the following error:
    Parser Error Message: Server tags cannot contain <% ... %> constructs.

    I wonder whether someone could please provide some insight on
    how I can solve this problem as I do need ids for the HTML
    input controls so that I can validate them with ASP.NET
    validation controls.

    It seems to me I am coming across what seem to be limitations
    in ASP.NET and what kind of programming constructs it supports...
    Any help with the above would be sincerely appreciated,

    Also, what would happen if several controls shared the id field.
    I thought this was not possible, but when I tried it asp.net did
    not complain.

    Thanks,

    Neil
     
    Neil Zanella, Jan 30, 2005
    #1
    1. Advertising

  2. Neil Zanella

    Scott M. Guest

    The problem is that you are trying to code ASP.NET with Classic ASP syntax.
    You should move away from inline code <% %> and move to either the
    code-behind paradigm of VS.NET or embed your programmatic code in server
    script blocks.

    <HTML>
    <HEAD>

    </HEAD>
    <BODY>
    <FORM runat=server id="Form1">


    </FORM>
    </BODY>

    <SCRIPT Langugage="VB" runat=server">
    Private Sub Page_Load(....) Handles MyBase.Load
    Dim I as Integer
    For I = 0 to N
    Dim inputControl as New System.Web.UI.WebControls.TextBox
    'Put code to configure new control and draw it on page here
    Next
    End Sub
    </SCRIPT>

    </HTML>

    "Neil Zanella" <> wrote in message
    news:...
    > Hello,
    >
    > I have a bunch of HTML input controls which I need to generate from
    > within a for loop. I would like them to have id attributes set to
    > values foo1, foo2, foo3, foo4, etc...
    >
    > Unfortunately it seems this is not possible because ASP.NET won't
    > let me use ASP tags within the values of the id attribute. So
    > for instance I cannot just do the following:
    >
    > <% for (i = 0; i < N; i++) { %>
    > <input id="foo<%= i %>" type="text" runat="server" />
    > <% } %>
    >
    > The reason is that IIS will issue the following error:
    > Parser Error Message: Server tags cannot contain <% ... %> constructs.
    >
    > I wonder whether someone could please provide some insight on
    > how I can solve this problem as I do need ids for the HTML
    > input controls so that I can validate them with ASP.NET
    > validation controls.
    >
    > It seems to me I am coming across what seem to be limitations
    > in ASP.NET and what kind of programming constructs it supports...
    > Any help with the above would be sincerely appreciated,
    >
    > Also, what would happen if several controls shared the id field.
    > I thought this was not possible, but when I tried it asp.net did
    > not complain.
    >
    > Thanks,
    >
    > Neil
     
    Scott M., Jan 30, 2005
    #2
    1. Advertising

  3. Neil Zanella

    Guest

    Thank you for your reply,

    What you are suggesting is defining everything programmatically
    and then adding the html tree structure to the rest of the html code
    with a call to somecontrol.Container.Add(code).

    While this works fine, it means I have to recode all of the HTML
    appearing in my code programmatically, which seems somewhat
    impractical and could lead to hard to maintain code. What I would
    rather prefer is a solution which makes use of asp:Repeater.

    Basically, asp:Repeater is designed to display controls from the
    result of a data set, which is what I want. Just that as I go through
    each repeater template I want to set the ID of my HTMLTextInput
    html control or equivalents so that I can reference them when
    I press the corresponding submit buttons.

    Is there a way to achieve this, using asp:Repeater which
    seems to be what the ASP.NET people designed for this
    kind of task???

    Thanks,

    Neil
     
    , Jan 30, 2005
    #3
  4. Neil Zanella

    Scott M. Guest

    > What you are suggesting is defining everything programmatically
    > and then adding the html tree structure to the rest of the html code
    > with a call to somecontrol.Container.Add(code).


    Actually, I think this is what you are suggesting based on your OP and the
    example you originally showed.

    > While this works fine, it means I have to recode all of the HTML
    > appearing in my code programmatically, which seems somewhat
    > impractical and could lead to hard to maintain code. What I would
    > rather prefer is a solution which makes use of asp:Repeater.
    >
    > Basically, asp:Repeater is designed to display controls from the
    > result of a data set, which is what I want.


    While a Repeater can do what you've said, it was designed to display
    repeating (templated) data from a result set. The fact that you can add
    controls is important, but not actually necessary for the use of the
    repeater.

    > Just that as I go through
    > each repeater template I want to set the ID of my HTMLTextInput
    > html control or equivalents so that I can reference them when
    > I press the corresponding submit buttons.
    >
    > Is there a way to achieve this, using asp:Repeater which
    > seems to be what the ASP.NET people designed for this
    > kind of task???


    I think you are coming at this backwards. It seems that you are trying to
    find a coding mechanism to back your way into what can be done without any
    of these problems.

    When you use a DataGrid, DataList and/or Repeater control, you can insert
    your own controls into the template (Item template, SelectedItem template,
    Header template, etc) and then you can access these controls in response to
    events that happen to your control. For example, if you were to add a
    textbox or label control to the EditItem template of a DataGrid. You could
    then access the textbox or label in response to some DataGrid event (like
    UpdateCommand) by determining which row the grid was editing (the "e"
    argument of the event handler tells us this) and then do something like
    this:

    Dim x as TextBox = CType(e.Item.Cells.FindControl(controlName), TextBox)

    I guess what I'm getting from your comments and your original sample code is
    that you are trying to use the new ASP.NET controls while programming in the
    old Classic ASP style. If you abandon Classic ASP completely and move to
    the ASP.NET event model and the separation of code from content paradigm,
    you will find that many of the corners one can code themselves into go away.
     
    Scott M., Jan 31, 2005
    #4
    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. Anshu
    Replies:
    5
    Views:
    408
    Howard
    Aug 14, 2006
  2. Uday Bidkar
    Replies:
    4
    Views:
    526
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  3. The Danny Bos

    IOError and Try Again to loop the loop.

    The Danny Bos, Jul 12, 2010, in forum: Python
    Replies:
    7
    Views:
    365
    Chris Rebert
    Jul 12, 2010
  4. a s
    Replies:
    16
    Views:
    4,767
    JustJohn
    Mar 8, 2011
  5. Isaac Won
    Replies:
    9
    Views:
    458
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page