0

I am trying to dynamically create a list of link buttons. I need the link buttons to have an onclick event. When I try to add the onclick event in C#, I get this error:

The name OnClick does not exist in the current context.

This is my code:

protected void fillList()
{
    List<PdfInfo> pdf = new List<PdfInfo>();
    pdf.Add(new PdfInfo { Path =  @"c:\TestCode\TestPdfs\", PdfName = "testPdf1.pdf"  });
    pdf.Add(new PdfInfo { Path = @"c:\TestCode\TestPdfs\", PdfName = "testPdf1.pdf" });
    pdf.Add(new PdfInfo { Path = @"c:\TestCode\TestPdfs\", PdfName = "testPdf1.pdf" });

    List<LinkButton> x = new List<LinkButton>();

    foreach(PdfInfo i in pdf)
    {
        x.Add(new LinkButton { ID = i.PdfName, Text = i.PdfName, OnClientClick = "linkBtnNewTab();", OnClick +="" });
    }
}

Is there no way to add an onclick event handler dynamically?

2
  • See my answer here: stackoverflow.com/a/42567762/5836671. But you cannot add it in the same way as a property. Commented Feb 20, 2024 at 14:08
  • Why do you think you need an <asp:LinkButton> instead of a HTML-only <a href=#"">? Consider that using your approach means that users won't be able to middle-click the link to open it in a new tab. Commented Feb 20, 2024 at 14:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.