I'm creating a table programmatically (HTML table by hand) with data from a query (DataSet).
One of the columnns has a link with the name of a file to download. The text shown is user-readable text, the link should have the actual long filename as parameter. I don't want to use an HREF as that would send it to another page. I want it to go to a method in the same page, that will read the file contents (actually an XLS or a PDF) from a SQL Server BLOB and give them the browser "download or open" dialog.
I have all the code for retrieving/downloading/displaying from the BLOB already done but I cannot get to trigger the process when they click on the link.
In the process (simplified) of drawing the HTML table I do this excerpt inside a for..next loop (C# 2.0):
int i = 0;
string sFileName = "filename.xls";
...
tCell = new TableCell();
tCell.HorizontalAlign = HorizontalAlign.Left;
tCell.ToolTip = "Click to download file.";
LinkButton lnkBtn = new LinkButton();
lnkBtn.ID = "lnkButton" + i.ToString();
lnkBtn.CommandName = "FILE";
lnkBtn.CommandArgument = sFileName;
lnkBtn.Text = "[text to show goes here]";
lnkBtn.Click += LinkButton1_Click;
tCell.Controls.Add(lnkBtn);
tRow.Cells.Add(tCell);
tblDownloadFiles.Rows.Add(tRow);
...
When clicking on the link the method
LinkButton1_Click(object sender, EventArgs e)
does not get called.
Line-by-line debugging shows the code doing a roundtrip to Page_load(), and doing a PostBack. The parameter (filename) is not seen and I can't see a way to retrieve it as the control is not pre-existing, but created in code and not seen in the debugger (lnkButton0)
Trace="true" reveals the following:
__EVENTTARGET ctl00$VOLPlaceHolder$lnkButton0
__EVENTARGUMENT
where "VOLPlaceHolder" is the simply name of the placeholder inside our master page and "lnkButton0" is the control I'm looking for, but as you can see
the argument list is empty.
I am sure it is something simple I'm overlooking. Any ideas?
Note:
LinkButton1_Click()
is named like this because it's a remnant from a previous grid that was replaced by the HTML table to gain more fine control. In either case, a LinkButton in the grid (as a template column), had the same problem mentioned above.