j'ai un GridView
nommé gvEmplAttachments qui a 3 colonnes:Fichier pas le téléchargement à partir GridView événement click
- id
- FileName
- CheminFichier
Chaque ligne a une LinkButton
cela permettra à l'utilisateur de télécharger le fichier, ce bouton est codé comme tel:
<asp:LinkButton id="lbViewFile" runat="server" CommandName="ViewFile" CommandArgument='<%# Container.DataItemIndex %>' >View</asp:LinkButton>
Le GridView est réglé avec les éléments suivants:
OnRowCommand ="gvEmplAttachments_OpenAttachment_RowCommand"
Alors qu'il exécutera la fonction dans le CodeBehind
Dans mon CodeBehind j'ai cette fonction:
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();
}
}
Mais le problème est lorsque je clique sur le bouton, je reçois cette erreur: Ma question est, qu'est-ce que je manque qui causerait la valeur nulle. Parce que la grille affiche le nom de fichier et le chemin de fichier corrects.
Comme vous l'avez mentionné votre ID ** Gridview ** est 'gvEmplAttachments', mais le code écrit pour saisir la La ligne de Gridview qui déclenche l'événement ** OnCommand ** a un ID grille différent 'GridViewRow gvr = gvUaSettings.Rows [rowindex];' est-ce correct ou le code est-il égaré? Peut-il ne pas avoir le bon Gridview Row. –
Copie possible de [Qu'est-ce qu'une exception NullReferenceException, et comment la réparer?] (Https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – VDWWD
@Rojalin Sahoo bonne prise! cela a corrigé le problème. - Si vous soumettez ceci comme réponse, je le marquerai pour vous. – Tommy