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