简体   繁体   中英

Run server side function on anchor tag click

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());
}

You can use an asp.net LinkButton instead:

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

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

EDIT : I notice that you also want this to open in a new window. To do that with a LinkButton, see this: http://forums.asp.net/t/1154673.aspx/1

Basically, you need to add the following to the server-side event handler:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM