tags:

views:

117

answers:

6

How can I append this

<% =i %>

variable onto this.

<asp:DropDownList ID="AdTitle" runat="server">

Cant have this for some reason.

<asp:DropDownList ID="AdTitle<% =i %>" runat="server">
+1  A: 

Is there more than one DropDownList? Do you know ahead of time how many there are? If so, you can set the id's ahead of time. If not, you can create the controls on the fly and give them whatever iD you want.

n8wrl
+5  A: 

Where does your "i" come from?

  • If it's part of a fixed or hard-coded loop, you really should just list out each control. If there are enough of these to make that very cumbersome, you should make this data driven somehow.
  • If it's data driven, you probably want a repeater control.
  • If it's pulled from a config file, put a place holder on the form and create the controls dynamically in your code-behind.
Joel Coehoorn
I probably should use a repeater control for this. Im an absolute beginner, how do I databind to the repeater control?
It depends on what your data looks like. At this point you'll likely do better starting a new question with that in mind. Don't forget to include all the relevant details, namely how you get your data, where your keeping it, and what it looks like.
Joel Coehoorn
ok. I will start a new question. thanks
A: 

if you find yourself trying to do such thing... something has to be very wrong ...

can I ask what is that you need to accomplish in order to give you a better way of doing that?

balexandre
+1  A: 
<asp:DropDownList ID="AdTitle<% =i %>" runat="server">

and

<asp:DropDownList ID='<= "AddTitle" + i %>' runat="server">

will not work because of the order in which asp.net processes and renders html. you can do this with a simple <select id='AddTitle<%=id%>'>

but you can't do it with asp.net controls

you are not allowed to dynamically set IDs of user and server control objects anyway because they are used to identify the controls to the code behind page on the server

Tion
+3  A: 

You shouldn't need to dynamically generate ids in this way.

Use a repeater (or any other repeating control with item templates):

<asp:Repeater ID="forEachItem" runat="server">
    <ItemTemplate>
        <asp:DropDownList ID="AdTitle" runat="server" />
        <%!-- any other content per item --%>
    </ItemTemplate>
</asp:Repeater>

Or generate your controls server side:

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

<%
    //...
    ph.Controls.Add( new DropDownList { Id = "AdTitle" + i } );
    //...
%>

If you use the repeater make sure you use databindings (<%#) rather than literals (<%=).

Keith
Brilliant. Thanks Keith. How can I bind data on the codefile?
Something like: forEachItem.DataSource = myDataTable; forEachItem.DataBind();
Keith
Then in your item template you can use bindings: <asp:Label Text='<%# Eval("ColumnName") %>' runat="server" />
Keith
A: 

If you really need to do something like this, you'll need to use an Expression Builder because of the way that the ASP.net engine parses the markup. I wrote a blog posting about them a while ago and there's some good links at the top. Possibly the most useful of them is the one going to 4GuysFromRolla.com.

I would add that the other suggestions here are almost certainly better ways of doing what you want to do, unless you have a very very specific reason for wanting to do it this way, which you've said you don't =)

Rob