【问题标题】:Onserverclick and onclick not workOnserverclick 和 onclick 不起作用
【发布时间】:2014-10-05 03:02:02
【问题描述】:

从我在 html 列表(表)中加载数据的代码中: 平均售价

<table id="tblClientes" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Nombre</th>
            <th>Razón Social</th>
            <th>Teléfono</th>
            <th>Email</th>
            <th></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <asp:Literal runat="server" ID="tableData"></asp:Literal>
    </tbody>

和.CS(代码隐藏)

tableData.Text += "<tr>";
                tableData.Text += "<td>" + cliente.Nombre + "</td>";
                tableData.Text += "<td>" + cliente.RazonSocial + "</td>";
                tableData.Text += "<td>" + cliente.Telefono + "</td>";
                tableData.Text += "<td>" + cliente.Email + "</td>";
                tableData.Text += "<td><a href='#' runat='server' onserverclick='btnCargarDatosCliente_Click' data-toggle='modal' data-target='#ventanaEmergente' id='btnCargarDatosCliente_" + cliente.Id.ToString() + "'>Editar</a></td>";
                tableData.Text += "<td><input type='button' data-toggle='modal' data-target='#ventanaAdvertencia' id='btnEliminarCliente_" + cliente.Id.ToString() + " value='Borrar' runat='server'/>Elimiar</td>";
                tableData.Text += "</tr>";

onserverclick='btnCargarDatosCliente_Click' 不调用事件。我确实尝试过 onclick,但还是一样。

有人知道我做错了吗?

【问题讨论】:

    标签: c# html asp.net


    【解决方案1】:

    这种方式是行不通的。如果您使用 runat="server" 将控件放在 aspx 表单上,则该控件将在服务器上处理,并且输出 html 会使用适当的 javascript 函数(如果您指定事件,例如 OnClick)返回给客户端,该函数会将表单发回。如果您像您一样生成字符串并将其分配给文字,则它会“按原样”呈现,因为服务器端处理已经发生,因此 runat="server" 在那里没有任何意义 - 它只是一个字符串。

    为了实现你想要的,我建议使用带有 LinkBut​​ton 控件的 Repeater 控件。网上有很多例子/教程如何做到这一点。

    【讨论】:

    • 那是正确的...如果您查看源代码,您会注意到源代码中的 runat="server"。如果像在 ASPX 页面中一样正确使用它而不是呈现数据,那么 runat="server" 将不会在您的视图源中呈现
    【解决方案2】:

    我认为您的问题可以使用LinkButtonHyperlink 而不是锚标记来解决。

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 2012-04-05
      • 2013-06-12
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2012-09-30
      • 2017-04-05
      • 1970-01-01
      相关资源
      最近更新 更多