C# 许多具有相同ID的文本框
我的代码如下:C# 许多具有相同ID的文本框,c#,html,asp.net,c#-4.0,for-loop,C#,Html,Asp.net,C# 4.0,For Loop,我的代码如下: <%for (int index = 1; index < 7; ++index) {%> <tr> <td> <div class="indicacao_gdp"> <asp:Tex
<%for (int index = 1; index < 7; ++index) {%>
<tr>
<td>
<div class="indicacao_gdp">
<asp:TextBox ReadOnly="true" ID="inp_txt_indicacao_<%= index %>" CssClass="inp_txt_indicacao" runat="server" MaxLength="12"></asp:TextBox>
</div>
</td>
<td>
<div class="codigo_debito_credito_gdp">
<asp:TextBox ReadOnly="true" ID="inp_txt_codigo_debito_credito_<%= index %>" CssClass="inp_txt_codigo_debito_credito" runat="server" MaxLength="2"></asp:TextBox>
</div>
</td>
<td>
<div class="descricao_debito_credito_gdp">
<asp:TextBox ReadOnly="true" ID="inp_txt_descricao_debito_credito_<%= index %>" CssClass="inp_txt_descricao_debito_credito" runat="server" MaxLength="2"></asp:TextBox>
</div>
</td>
<td>
<div class="valor_debito_credito_gdp">
<asp:TextBox ReadOnly="true" ID="inp_txt_valor_debito_credito_inteiro_<%= index %>" CssClass="inp_txt_valor_inteiro" runat="server" MaxLength="8"></asp:TextBox>
,
<asp:TextBox ReadOnly="true" ID="inp_txt_valor_debito_credito_decimal_<%= index %>" CssClass="inp_txt_valor_decimal" runat="server" MaxLength="2"></asp:TextBox>
</div>
</td>
</tr>
<%}%>
,
但是,代码不起作用
Parser Error Message: 'inp_txt_indicacao_<%= index %>' is not a valid identifier.
Parser错误消息:“inp\u txt\u indicaco\u”不是有效的标识符。
我是如何解决的?使用
占位符
控件,并从代码隐藏处动态添加文本框
控件。在代码隐藏中,您可以轻松设置ID和任何其他您喜欢的属性
请参阅。为什么需要这样做?如果使用runat=“server”,控件将永远不会具有相同的ID。如果使用类将这些文本框包装在容器中,则可以通过jQuery轻松从这些元素获取数据 使用runat=“server”时,您正在创建.net对象,而不是实际的html 您有两个选项,可以使用a并将id设置为id=“inp_txt_indicaco”(.net将负责使其唯一) 或是代替
使用
中继器
或网格视图
您不能在ID属性上执行
,尽管CssClass等其他属性允许这种绑定。我认为您的意思是将相同的索引
值附加到多个文本框
ID,而实际上没有具有相同ID的文本框
控件。@TimSchmelter我尝试使用Repeater,但是也不行。@FabianoLothor告诉我们它不行对我们没有帮助。你应该1)提供你正在使用的代码2)具体解释什么不起作用。您是否收到错误,输出是否不正确,如果是,应该是什么,等等。好的,但是。。。我需要在代码隐藏中设置文本框值。我从来没有在类似的情况下使用转发器。更好的形式是使用FindControl函数。这比使用转发器
/GridView
之类的东西要困难得多。如果你想生成特定的id,我相信这是最简单的方法。如果TextBox
控件没有数据绑定怎么办?我并不认为有必要使用特定的ID,但OP对Repeater
或GridView
都不熟悉,因此不知道它们是一个选项。如果没有任何数据绑定到它们,那么就不能将任何字段绑定到这些控件。如果没有输入数据,而只有输出数据,则可以将空白值绑定为输入,以便查看输出。@Servy Hi,我使用GridView中的FindControl函数解决了问题。谢谢。
<input type="textbox" id="inp_txt_indicacao_<%= index %>" />
Public Class TestClass
Public Sub New(ByVal v1 As String, ByVal v2 As String)
Value1 = v1
Value2 = v2
End Sub
Public Property Value1() As String
Public Property Value2() As String
End Class
Dim tc As New List(Of TestClass)
tc.Add(New TestClass("aa", "bb"))
tc.Add(New TestClass("cc", "dd"))
tc.Add(New TestClass("ee", "ff"))
rpData.DataSource = tc
rpData.DataBind()
<asp:Repeater ID="rpData" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:TextBox ID="txtValue1" Text='<%# Bind("Value1") %>' runat="server" /></td>
<td><asp:TextBox ID="txtValue2" Text='<%# Bind("Value2") %>' runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>