I've got a few .pdf files I have on my server windows 2003 and I would like to know how many times each file has been downloaded.
I tried this solution in my code-behind , I don't have error but the count download not is incremented.
What's the best way to track this?
My code-behind below.
Any help would be appreciated, thank you in advance.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeader="true"
CssClass="mGrid" EmptyDataText="Empty" DataKeyNames="ID">
<AlternatingRowStyle CssClass="altrows" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<center>
<asp:Label ID="ID" runat="server" Text='<%# Eval("ID").ToString() %>'></asp:Label>
</center>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ItemStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<center>
<asp:HyperLink ID="Download" runat="server" NavigateUrl='<%# Eval("Download").ToString() %>'
ImageUrl="/images/download.gif" BorderStyle="None"
ForeColor="Transparent" OnClick="Button_Click" ToolTip="Download">
</asp:HyperLink>
</center>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="View" HeaderText="View" />
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink Download = (HyperLink)e.Row.FindControl("Download");
Label ID = (Label)e.Row.FindControl("ID");
ID.Text = Request.QueryString["ID"];
}
}
protected void Button_Click(object sender, EventArgs e)
{
HyperLink Download = (HyperLink)sender;
GridViewRow row = (GridViewRow)Download.NamingContainer;
Label ID = (Label)row.FindControl("ID");
using (OdbcConnection conn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
{
sql1 = " UPDATE doTable SET View = View + 1 " +
" WHERE ID = ?; ";
using (OdbcCommand command =
new OdbcCommand(sql1, conn))
{
try
{
command.Parameters.AddWithValue("param1", ID.Text.ToString());
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
command.Connection.Close();
}
}
}
}
This
HyperLink Download = (HyperLink)sender;
GridViewRow row = (GridViewRow)Download.NamingContainer;
Label ID = (Label)row.FindControl("ID");
does not point to the row where the button is clicked and I suppose you increment the counter always for the first row only.
I'd recommend to change HyperLink to ImageButton and use its CommandArgument property
<asp:ImageButton runat="server" ID="Download"
ImageUrl="/images/download.gif" CommandArgument="<%# Eval("ID") %>" />
Then in the code-behind
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string id = e.CommandArgument;
...
}
See more at http://msdn.microsoft.com/en-us/library/bb907626(v=vs.100).aspx
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.