【问题标题】:Many textboxes with the same ID许多具有相同 ID 的文本框
【发布时间】:2013-08-15 18:42:49
【问题描述】:

我有以下代码:

                <%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.

我是怎么解决的?

【问题讨论】:

  • 使用Repeater 或GridView
  • 您不能在 ID 属性上执行 &lt;%= index %&gt;,尽管 CssClass 等其他属性允许这种类型的绑定。
  • 我认为您的意思是将相同的 index 值附加到多个 TextBox ID 上,而实际上并没有具有相同 ID 的 TextBox 控件。
  • @TimSchmelter 我尝试使用中继器,但也没有用。
  • @FabianoLothor 告诉我们它不起作用对我们没有帮助。您应该 1) 提供您正在使用的代码 2) 具体地解释什么不起作用。您是否收到错误,输出是否不正确,如果是,应该是什么,等等。

标签: c# html asp.net c#-4.0 for-loop


【解决方案1】:

使用PlaceHolder 控件并从代码隐藏动态添加TextBox 控件。在代码隐藏中,您可以轻松设置 ID 和您喜欢的任何其他属性。

见MSDN: Add Controls to an ASP.NET Web Page Programmatically。

【讨论】:

  • 这比使用Repeater/GridView 之类的东西更难做和使用。
  • 如果你想生成特定的 id,我相信这是最简单的方法。如果TextBox 控件没有数据绑定怎么办??
  • 我真的不认为有特定 ID 的迫切需要,而是 OP 不熟悉 Repeater 或 GridView,因此不知道它们是一个选项.如果您没有任何数据要绑定到它们,那么您可以不将任何字段绑定到这些控件。如果没有输入数据,而只有输出数据,你可以只绑定空白值作为输入,这样你就可以查看输出了。
  • @Servy 嗨,我使用 GridView 中的 FindControl 函数解决了我的问题。谢谢。
【解决方案2】:

为什么需要这样做?如果您使用 runat="server" 控件将永远不会有相同的 ID。如果将这些文本框包装在一个带有类的容器中,则可以通过 jQuery 从这些元素中轻松获取数据。

【讨论】:

    【解决方案3】:

    当您使用 runat="server" 时,您创建的是 .net 对象而不是实际的 html。

    您有两个选择,您可以使用 Repeater 并将 id 设置为 ID="inp_txt_indicacao"(.net 会注意使其唯一)。

    或者你把

    <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>
    

    【讨论】:

    • 好的,但是...我需要在后面的代码中设置文本框的值。而且我从来没有在类似的情况下使用中继器。
    • 更好的形式是使用 FindControl 函数。
    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多