asp.net方法不能在gvresult事件行命令(c#)中工作

本文关键字:命令 工作 事件 方法 net 不能 gvresult asp | 更新日期: 2023-09-27 18:13:02

这是我的行命令

protected void gvResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
            Response.Clear();
            Response.ContentType = "application/octect-stream";
            Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
            Response.TransmitFile(Server.MapPath("~/Files/") + e.CommandArgument.ToString());
            Response.End();
            //updateCountDownloaded(e.CommandArgument.ToString());
            updateCountDownloaded("1");
        }        
    }
    public void updateCountDownloaded(string iDFile)
    {
        files.updateCountDownloaded(iDFile);
        BindTaskList();
    }

,这是文件

public void updateCountDownloaded(string IdFile)
    {
        SqlCommand command = new SqlCommand();
        command.CommandType = CommandType.Text;
        command.CommandText = string.Format(
            "UPDATE {0} SET Count = Count + 1 WHERE IdFile = @IdFile"
            ,tableName);
        command.Parameters.AddWithValue("@IdFile", IdFile);
        datalayer.setData(command);
    }

当我尝试运行" updatecountdownloads("1");"在Page_load,它工作正常。但是为什么它不能在行命令方法中工作?我该如何解决呢?提前致谢

update: no exception, no error, but my table data not updated

asp.net方法不能在gvresult事件行命令(c#)中工作

在行命令中,您正在将文件刷新为响应,并通过调用response . end来完成进一步的执行。你可以尝试在执行其他代码之前调用你的函数,如下所示。

protected void gvResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
           //updateCountDownloaded(e.CommandArgument.ToString());
            updateCountDownloaded("1");
            Response.Clear();
            Response.ContentType = "application/octect-stream";
            Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
            Response.TransmitFile(Server.MapPath("~/Files/") + e.CommandArgument.ToString());
            Response.End();
        }        
    }