0
我有一个GridView
名为gvEmplAttachments有3列:文件不是从GridView控件的单击事件下载
- ID
- 文件名
- 文件路径
每一行都有一个LinkButton
这将允许用户下载该文件,该按钮编码如下:
<asp:LinkButton id="lbViewFile" runat="server" CommandName="ViewFile" CommandArgument='<%# Container.DataItemIndex %>' >View</asp:LinkButton>
GridView控件设置下列要求:
OnRowCommand ="gvEmplAttachments_OpenAttachment_RowCommand"
因此,它会在代码隐藏
执行的功能在我隐藏我有这样的功能:
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
我的问题是,我错过了什么会导致空值。 由于网格显示正确的FileName和FilePath。
至于你提到你**的GridView ID **是'gvEmplAttachments',但你写的代码抢触发** OnCommand **事件的Gridview行有不同的GridView ID'GridViewRow gvr = gvUaSettings.Rows [rowindex];'这是正确的还是代码错位?可能它没有得到正确的Gridview Row。 –
[什么是NullReferenceException,以及如何解决它?]可能的重复(https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it ) – VDWWD
@Rojalin Sahoo好抓!纠正了这个问题。 - 如果您将此作为答案提交,我会为您标记。 – Tommy