2

I know that we can bind the data to each control within ItemTemplate as follow:

<ItemTemplate>
    <asp:TextBox runat="server"
        Text='<%# Eval("LabelText") %>' />
</ItemTemplate>

However, I found no way to concatenate a string prefix with the data value to form a unique string identifier. The following code shows my idea, but it doesn't work.

<ItemTemplate>
    <asp:TextBox runat="server"
         ID='TextBox_<%# Eval("LabelID") %>'
         ValidationGroup = 'VVG_<%# Eval("LabelGroup") %>'
         Text='<%# Eval("LabelText") %>' />
</ItemTemplate>
R0MANARMY
  • 17,173
  • 4
  • 57
  • 81
William Choi
  • 5,471
  • 6
  • 29
  • 48

2 Answers2

2

Try this

ID = '<%# "Text_" + Eval("LabelID") %>'

ValidationGroup = '<%# "VVG_" + Eval("LabelGroup") %>'

EDIT:

ID cannot be assigned in this fashion for server side controls. You can assign ID for simple form controls such as <input type="text"... />. Also take a look at Control.ClientIDMode (ASP.NET 4).

Bala R
  • 101,930
  • 22
  • 186
  • 204
  • I don't thinks this will work, when I tried to run an example with it I got an error saying **Parser Error Message: The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: ``** – R0MANARMY May 16 '11 at 02:42
  • @R0MANARMY did you try it in the ItemTemplate of a repeater? – Bala R May 16 '11 at 02:45
  • Inside the ItemTemplate part of a repeater, I can post my code if you'd like. – R0MANARMY May 16 '11 at 02:47
  • @Bala R: I guess more specifically, I don't think (according to the error) it'll work for the ID field but should be fine for others. – R0MANARMY May 16 '11 at 03:14
0

Try this:

<asp:TextBox runat="server"         
    ID='<%# String.Format("TextBox_{0}",Eval("LabelID")) %>'         
    ValidationGroup = '<%# String.Format("WG_{0}",Eval("LabelGroup")) %>'         
    Text='<%# Eval("LabelText") %>' />
Dmitry
  • 2,999
  • 1
  • 13
  • 25