Onclick event in buttons created dynamically

0

I am creating a table and two columns I have to put some buttons to edit and delete but what happens is that the table is created well but pressing the buttons does not find the onclick event.

Does anyone know what is going on?

Every time I press the buttons I get the error that I put down.

I have tried with button, asp: button, linkbutton, a, etc and nothing detects me onclik

 protected void Page_Load(object sender, EventArgs e)
    {
        ObtenerActividadesColaborador();
    }

    public void ObtenerActividadesColaborador()
    {
        LogicaNegocioRequerimiento logicaNegocioRequerimiento = new LogicaNegocioRequerimiento();
        var Actividades = logicaNegocioRequerimiento.ObtenerListaActividadesColaborador("jramireza");
        StringBuilder Builder = new StringBuilder();

        for (int i = 0; i < Actividades.Count; i++)
        {
            Builder.Append("<tr>");
            Builder.Append("<td><asp:Button id='btnActualizar" + i + "' class='btn btn-primary' OnClick='btnActualizar_Click' runat='server' /><i class='fa fa-pencil'></i> Editar</td>");
            Builder.Append("<td><asp:Button id='btnBorrar" + i + "' class='btn btn-danger' OnClick='btnBorrar_Click' runat='server' /><i class='fa fa-trash-o'></i> Borrar</td>");
            Builder.Append("<td>" + Actividades[i].IdRequerimiento + "</td>");
            Builder.Append("<td>" + Actividades[i].DscSistema + "</td>");
            Builder.Append("<td>" + Actividades[i].DeRequerimiento + "</td>");
            Builder.Append("<td>" + Actividades[i].CanTiempoAtencion + "</td>");
            Builder.Append("<td>" + Actividades[i].FecRegistroActividad + "</td>");
            Builder.Append("</tr>");
        }
        ListaActividades.InnerHtml = Builder.ToString();
    }

    protected void btnBorrar_Click(object sender, EventArgs e)
    {
        int c = 1;

    }

    protected void btnActualizar_Click(object sender, EventArgs e)
    {
        int c = 1;

    }

ERROR

 Uncaught ReferenceError: btnActualizar_Click is not defined at HTMLUnknownElement.onclick (Actividades:137)
    
asked by Alex 10.07.2018 в 00:12
source

2 answers

0

This article can help you:

Events in dynamically added controls

  

"You will see that when creating dynamic controls you must place this in the Init or Load events and in each post you must assign the event, otherwise you will lose the assignment and consequently the event will not be launched."

     

"The truth is that if you can avoid creating controls like this, use the Repeater"

I hope it serves you!

    
answered by 10.07.2018 в 16:22
0

In the end I had to do it with javascript,

so create the button

 Builder.Append("<td><input type='button' class='btn btn-primary' value='Actualizar' id='BtnActualizar" + (i + 1) + "' onclick='btnActualizar_Click("+ Actividades[i].ConEncRegistroActividad + ")'</td>");

and

Then I did the JS function

function btnUpdate_Click (Id) {            //CODE         }

    
answered by 12.07.2018 в 17:38