4

I need to track when a file link is clicked on so I built a server side function that writes to a SQL DB when an anchor tag is clicked. It's not firing or opening the file. Here is my code:

HTML

<a href="pdf/Access2013.pdf#zoom=100" runat="server" onServerClick="AccessFile_Click" target="_blank"><img src="img/pdf_icon.png" border="0" /></a>

SERVER CODE

protected void AccessFile_Click(object sender, EventArgs e)
{
  App_Code.bi.LogFileDownload("Access File", Session["UserID"].ToString());
}
4

1 回答 1

5

您可以改用 asp.net LinkBut​​ton

<asp:LinkButton ID="MyLink" runat="server" OnClick="AccessFile_Click" Text="Click Here"></asp:LinkButton>

参考:http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.aspx

编辑:我注意到您还希望它在新窗口中打开。要使用 LinkBut​​ton 执行此操作,请参阅:http ://forums.asp.net/t/1154673.aspx/1

基本上,您需要将以下内容添加到服务器端事件处理程序中:

string newWindowUrl = "pdf/Access2013.pdf#zoom=100";   
 string javaScript =
  "<script type='text/javascript'>\n" +
  "<!--\n" +
  "window.open('" + newWindowUrl + "');\n" +
  "// -->\n" +
  "</script>\n";
 this.RegisterStartupScript("", javaScript);
于 2013-08-30T15:00:13.760 回答