Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > input control id and for loop

Reply
Thread Tools

input control id and for loop

 
 
Neil Zanella
Guest
Posts: n/a
 
      01-30-2005
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
 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      01-30-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
 
nzanella@cs.mun.ca
Guest
Posts: n/a
 
      01-30-2005

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

 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      01-31-2005
> 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.


 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Count bits in VHDL, with loop and unrolled loop produces different results a s VHDL 16 03-08-2011 05:35 PM
IOError and Try Again to loop the loop. The Danny Bos Python 7 07-12-2010 04:34 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
I have just creted a loop to sort integers in the input loop who's it Anshu C++ 5 08-14-2006 03:13 PM



Advertisments