单击事件时未从 GridView 下载文件
File not downloading from GridView on click event
我有一个名为 gvEmplAttachments 的 GridView
,它有 3 列:
- 编号
- 文件名
- 文件路径
每一行都有一个 LinkButton
允许用户下载文件,该按钮编码如下:
<asp:LinkButton id="lbViewFile" runat="server" CommandName="ViewFile" CommandArgument='<%# Container.DataItemIndex %>' >View</asp:LinkButton>
GridView 设置如下:
OnRowCommand ="gvEmplAttachments_OpenAttachment_RowCommand"
以便执行CodeBehind中的函数
在我的 CodeBehind 中我有这个函数:
protected void gvEmplAttachments_OpenAttachment_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewFile")
{
//Get rowindex
int rowindex = Convert.ToInt32(e.CommandArgument);
//Get the Row
GridViewRow gvr = gvUaSettings.Rows[rowindex];
//Get the Needed Values
Label lblPath = gvr.FindControl("lblFilePath") as Label;
Label lblName = gvr.FindControl("lblFileName") as Label;
//String The values
string fileName = lblName.Text;
string filePath = Server.MapPath(lblPath.Text);
//Should Download the file
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/x-unknown";
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
response.TransmitFile(filePath);
response.Flush();
response.End();
}
}
但问题是当我单击按钮时出现此错误:
Object reference not set to an instance of an object
我的问题是,我遗漏了什么会导致空值。
因为网格显示正确的文件名和文件路径。
正如您提到的,您的 Gridview ID 是 gvEmplAttachments,但是您编写的用于获取触发 OnCommand 事件的 Gridview 行的代码具有不同的 Gridview ID
GridViewRow gvr = gvUaSettings.Rows[rowindex];
。
您可以尝试以下代码获取触发命令事件的行:
GridViewRow gvr = gvEmplAttachments.Rows[rowindex];
我有一个名为 gvEmplAttachments 的 GridView
,它有 3 列:
- 编号
- 文件名
- 文件路径
每一行都有一个 LinkButton
允许用户下载文件,该按钮编码如下:
<asp:LinkButton id="lbViewFile" runat="server" CommandName="ViewFile" CommandArgument='<%# Container.DataItemIndex %>' >View</asp:LinkButton>
GridView 设置如下:
OnRowCommand ="gvEmplAttachments_OpenAttachment_RowCommand"
以便执行CodeBehind中的函数
在我的 CodeBehind 中我有这个函数:
protected void gvEmplAttachments_OpenAttachment_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewFile")
{
//Get rowindex
int rowindex = Convert.ToInt32(e.CommandArgument);
//Get the Row
GridViewRow gvr = gvUaSettings.Rows[rowindex];
//Get the Needed Values
Label lblPath = gvr.FindControl("lblFilePath") as Label;
Label lblName = gvr.FindControl("lblFileName") as Label;
//String The values
string fileName = lblName.Text;
string filePath = Server.MapPath(lblPath.Text);
//Should Download the file
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/x-unknown";
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
response.TransmitFile(filePath);
response.Flush();
response.End();
}
}
但问题是当我单击按钮时出现此错误:
Object reference not set to an instance of an object
我的问题是,我遗漏了什么会导致空值。 因为网格显示正确的文件名和文件路径。
正如您提到的,您的 Gridview ID 是 gvEmplAttachments,但是您编写的用于获取触发 OnCommand 事件的 Gridview 行的代码具有不同的 Gridview ID
GridViewRow gvr = gvUaSettings.Rows[rowindex];
。
您可以尝试以下代码获取触发命令事件的行:
GridViewRow gvr = gvEmplAttachments.Rows[rowindex];