Gonzalo Rivera
Gonzalo Rivera

Reputation: 183

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?

Upvotes: 0

Views: 608

Answers (2)

Anup Sharma
Anup Sharma

Reputation: 2083

I think your problem can be solved using LinkButton or Hyperlink instead of anchor tag.

Upvotes: 0

PrzemG
PrzemG

Reputation: 795

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.

Upvotes: 1

Related Questions