简体   繁体   中英

Onserverclick and onclick not work

From my code behind im load data in html list (table): ASP

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

and .CS(Code Behind)

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

The onserverclick='btnCargarDatosCliente_Click' not call the event. i did try with onclick but same.

Anyone know I'm doing wrong?

It does not work that way. If you place control on the aspx form with runat="server" that control is processed on the server and output html is returned to the client with proper javascript function (if you specify event like for example OnClick) that posts the form back. If you generate string like you do and assign it to literal it is render 'as it is' as server-side processing already has taken place so runat="server" has no meaning there - it is just a string.

To achieve what you want I suggest using Repeater control with LinkButton control. There is a lot of examples/tutorial on the web how to do it.

我认为您可以使用LinkButtonHyperlink代替锚标记来解决您的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM