在文本框上动态绑定ID

时间:2014-02-10 10:04:10

标签: asp.net eval

我有

<asp:TextBox ID="txtBidAmmount<%#Eval("id") %>" Width="250" runat="server"></asp:TextBox>

这不起作用,我已经阅读了一些选项,我必须用"替换',所以我做了:

<asp:TextBox ID='txtBidAmmount<%#Eval("id") %>' Width="250" runat="server"></asp:TextBox>

但我明白了:

Parser Error Message: 'txtBidAmmount<%#Eval("id") %>' is not a valid identifier.

3 个答案:

答案 0 :(得分:0)

您无法动态设置ID。也就是说,如果您有runat="server",那么ID必须是 COMPILE TIME CONSTANT

答案 1 :(得分:0)

您可以尝试使用ClientIDMode="Predictable"作为转发器/ gridview吗?

Example from CodeProject article

<asp:GridView ID="EmployeesNoSuffix" runat="server" 
    AutoGenerateColumns="false" ClientIDMode="Predictable">
<Columns>
    <asp:TemplateField HeaderText="ID">
         <ItemTemplate>
              <asp:Label ID="EmployeeID" runat="server" Text='<%# Eval("ID") %>' />
         </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name">
         <ItemTemplate>
              <asp:Label ID="EmployeeName" 
        runat="server" Text='<%# Eval("Name") %>' />
         </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

输出HTML

<table id="EmployeesNoSuffix" style="border-collapse: collapse" 
    cellspacing="0" rules="all" border="1">
  <tbody>
    <tr>
       <th scope="col">ID</th>
       <th scope="col">Name</th>
    </tr>
    <tr>
       <td><span id="EmployeesNoSuffix_EmployeeID_0">1</span></td>
       <td><span id="EmployeesNoSuffix_EmployeeName_0">EmployeeName1</span></td>
    </tr>
    ...
    <tr>
      <td>
         <span id="EmployeesNoSuffix_EmployeeID_8">9</span>
      </td>
      <td>
         <span id="EmployeesNoSuffix_EmployeeName_8">EmployeeName9</span>
      </td>
    </tr>
  </tbody>
</table>

答案 2 :(得分:0)

试试这个

ID='<%# "txtBidAmmount" + eval("id") %>'

ID='<%# Eval("id", "txtBidAmmount {0}") %>'