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

时间:2014-08-08 07:54:46

标签: c# asp.net pdf

我的服务器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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这个

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/en-us/library/bb907626(v=vs.100).aspx

了解详情