473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

input control id and for loop

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
Nov 19 '05 #1
3 2218
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" <nz******@gmail.com> wrote in message
news:1a**************************@posting.google.c om...
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

Nov 19 '05 #2

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

Nov 19 '05 #3
> 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.
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If,...
1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
1
by: Neil Zanella | last post by:
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... ...
0
by: ntuser_man | last post by:
Howdy I'm trying to validate the content of a csv uploaded to a web page. I imported the csv into a DataSet and now I want to loop through all the elements in the DataSet to validate that each is...
1
by: Rob Meade | last post by:
Hi all, I have a loop in my code which builds the controls on the page. I at one stage need to add some hidden input controls dynamically, I have achieved this, and I have set their...
0
by: Diogo Bastos | last post by:
Hello, I'm fairly used to working with Python but it's the first time I'm trying to use Tkinter so I'm running into a problem. I'm using three python scripts with Tkinter GUIs and a fourth...
5
by: Kavya | last post by:
I saw these two ways for validating input First Way -------------- #include <iostream> #include <limits> using namespace std; int main() {
6
by: parez | last post by:
Is there anyway i can find out if a control is an input control (textbox,radiobutton,checkbox,dropdown etc) and not a panel,group box etc. I am trying to implement "You have unsaved data on the...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.