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">
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">
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.
Where does your "i" come from?
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?
<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
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 (<%=
).
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 =)