Run the server side function on the binding label

I need to track when a file link is clicked, so I built a function on the server side that writes to SQL DB when I click on the anchor tag. It does not shoot or open 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
source share
1 answer

Instead, you can use asp.net LinkButton :

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

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

EDIT . I noticed that you also want this to open in a new window. To do this using 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); 
+6
source

Source: https://habr.com/ru/post/1499823/


All Articles