tags:

views:

162

answers:

4

hi guys i am using this code in my aspx page

<%for (int i=o;i<5;i++)
{%>
  <asp: link button id=i text=i/>

<%}%>

at it's producing five link buttons like

i i i i i

but i just want five link buttons like that

1 2 3 4 5

with id=1,2,3,4,5 respectively

how can I can implement that

+4  A: 

This isn't exactly the best way to do things in ASP.Net. Dynamic controls have many gotchas, and you just found one of them. This code is a little better (make sure the page calls DataBind() at some point):

<%for (int i=0;i<5;i++)
{%>
  <asp:LinkButton id="<%# i%>" runat="server" text="<%#i%>" />

<%}%>

but you'll find that events and changes don't wire up the way you'd expect them to, if it works at all. With only five of them, it's better to just list them out by hand, and things will be much easier down the road. Or, if they came from a datasource somewhere use a repeater and create them that way.

Really, you don't want to mix code into your aspx markup at all if you can help it. Leave it for the code behind!

Joel Coehoorn
Yeah, need to wake up yet this morning. Post originally had a bunch of typos, too, I amazed I wasn't downvoted. The whole pattern is wrong, and I so avoid it enough I'm not even 100% what the exact correct syntax is without trying it a few times somewhere.
Joel Coehoorn
You cannot set the ID of a control this way. You will get an exception : The ID property of a control can only be set using the ID attribute in the tag and a simple value.
Darin Dimitrov
You will also get an exception because the variable "i" is not known in the context of the Text property even if you call DataBind.
Darin Dimitrov
A: 

YOu need to actually print out the value of I on the server side.

<%for (int i=o;i<5;i++) {%> 
    <ASP:LinkButton Id="<%=i%>" Text="<%=i %>" />
<%}%>
aquillin
<%i%> produces nothing, you should write it as <%= i%> or <%# i%>
Canavar
Yeah, I just mistyped it... you could have edited it though :) instead of critisizing.
aquillin
+7  A: 

Put a placeholder in the page where you want the links:

<asp:PlaceHolder ID="LinkContainer" runat="server" />

In the Page_Load method in code behind, you put the links in the placeholder:

for (int i = 1; i <= 5; i++) {
   LinkButton link = new LinkButton();
   link.ID = "Link" + i.ToString();
   link.Text = i.ToString();
   LinkContainer.Controls.Add(link);
}

This way the controls will be created early in the page cycle, and it's possible to hook up server side events to them if you like.

(Note that I added a prefix to the id. Having an id that is only digits can cause problems in some situations.)

Guffa
+2  A: 

You need to be careful how you actually render this. Note that HTML ids that start with a number are illegal according to the specification. I would prepend some alphabetic character to the id value to be sure that it is both legal HTML and that the control id generated is legal for ASP.NET. It's unclear to me whether it's legal or not to use just a numeric value for a control id. Note that ASP.NET control id characters must be alphanumeric or an underscore.

HTML Reference:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

ASP.NET Reference

Note: Only combinations of alphanumeric characters and the underscore character ( _ ) are valid values for this property. Including spaces or other invalid characters will cause an ASP.NET page parser error.

tvanfosson