input control id and for loop

N

Neil Zanella

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
 
S

Scott M.

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

nzanella

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
 
S

Scott M.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top