Problem with onclick event of a button on asp.net

0

I'm making a dynamic table in ASP . This table has buttons in one of the cells.

I have the following code in page_load :

StringBuilder htmlStr = new StringBuilder();
htmlStr.Append(
 "<asp:button onclick='bttn_pdf_Click' class='btn btn-warning btn-xs' ID='Bttn_PDF'>PDF</asp:button>"
);
PlaceHolder1.Controls.Add(new Literal { Text = htmlStr.ToString() });

This is only the part of the buttons that is where I have problems, the idea is that by clicking on it you download a PDF , but the problem is that when you click it shows the following error in the console :

Uncaught ReferenceError: bttn_pdf_Click is not defined
 at HTMLUnknownElement.onclick (VM184 panel.aspx:1)
    
asked by david arellano 11.03.2017 в 18:02
source

2 answers

0

In the code you are showing, you are trying to execute the function on the client side (in the browser) and you will need javascript. In this case:

  • Normally, the handler of the onclick event is a function and the parentheses are missing. Therefore the code should be onclick="bttn_pdf_Click()
  • You will have to create in javascript the function bttn_pdf_Click () of the form:

    function bttn_pdf_Click(){
      //Código que realice la llamada al servidor para generar el PDF
    }
    
  • Perhaps it would be better for you if the button were to execute the call directly on the server. In this case you should put onserverclick="bttn_pdf_Click" (and of course you'll have to have declared this function in your asp code). You can see the documentation of events in click on server here: link

        
    answered by 14.04.2017 в 11:19
    0

    To add elements dynamically to the interface, it is preferable to create a method that inserts the element in the document:

    public void AgregarBoton()
    {
        Button nuevo_boton = new Button();
        nuevo_boton.ID = "Bttn_PDF";
        nuevo_boton.Text = "PDF";
        nuevo_boton.Click += new EventHandler(bttn_pdf_Click);
        PlaceHolder1.Controls.Add(nuevo_boton);
    }
    
        
    answered by 19.10.2018 в 07:19