简体   繁体   English

在锚标签点击上运行服务器端功能

[英]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. 我需要跟踪单击文件链接的时间,因此我构建了一个服务器端函数,在单击锚标记时写入SQL DB。 It's not firing or opening the file. 它不会触发或打开文件。 Here is my code: 这是我的代码:

HTML 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.net LinkBut​​ton代替:

<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 参考: 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 要使用LinkBut​​ton,请参阅: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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