繁体   English   中英

使用ASP.NET跟踪下载文件.pdf

[英]Track downloads files .pdf with ASP.NET

我的Windows 2003服务器上有一些.pdf文件 ,我想知道每个文件已下载了多少次。

我在后台代码中尝试了此解决方案,但没有错误,但下载计数未增加。

追踪此事的最佳方法是什么?

我的代码在下面。

任何帮助将不胜感激,在此先感谢您。

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

这个

HyperLink Download = (HyperLink)sender;
GridViewRow row = (GridViewRow)Download.NamingContainer;
Label ID = (Label)row.FindControl("ID");

并不指向单击按钮的行,我想您总是只对第一行增加计数器。

我建议将HyperLink更改为ImageButton并使用其CommandArgument属性

<asp:ImageButton runat="server" ID="Download" 
     ImageUrl="/images/download.gif" CommandArgument="<%# Eval("ID") %>" />

然后在代码背后

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    string id = e.CommandArgument;
    ...
}

http://msdn.microsoft.com/zh-cn/library/bb907626(v=vs.100).aspx中查看更多信息

暂无
暂无

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

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