Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<asp:GridView ID="GridImport" runat="server" AutoGenerateColumns="false" Width="99%" Height="50%"
                                   AllowPaging="true" GridLines="None" Style="padding: 15px; text-align: left; overflow: scroll;
                                   font-family: Arial; font-size: 11pt;" ShowHeaderWhenEmpty="true" PageSize="5"
                                   CssClass="Grid_LE" HeaderStyle-CssClass="Grid_Head" EmptyDataText = "No files Imported">
               <Columns>
                   <asp:BoundField DataField="Text" HeaderText="File Name" />
                   <asp:TemplateField>
                       <ItemTemplate>
                        <%--   <asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile" ></asp:LinkButton> --%>
                             <asp:ImageButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 24px; height: 24px;" ImageUrl="~/Images/download.png" OnClick="DownloadFile" />   <%--CommandName="Upload"--%>
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField>
                       <ItemTemplate>
                           <%--<asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" /> --%>
                             <asp:ImageButton ID="lnkDelete" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 24px; height: 24px;" ImageUrl="~/Images/cancel.png" OnClick="DeleteFile" />
                       </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>



HERE is my DownloadFile function
--------------------------------------

C#
protected void DownloadFile(object sender, EventArgs e)
       {
           string filePath = (sender as ImageButton).CommandArgument;
           Response.ContentType = ContentType;
           Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
           Response.WriteFile(filePath);
           Response.End();
       }







WHILE CLICKING ON DOWNLOAD BUTTON I AM GETTING THIS ERROR

XML
Server Error in '/' Application.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.



HOW CAN I SOLVE THIS?
Posted

This error may be thrown when you click on your command and the Page_load is being ran again. Bind data only the first time when your page load, like below
C#
if (!Page.IsPostBack)
{ 
   //bind data to  GridImport
}


hope this helps, there may be many other reasons for the same error, this is the common mistake I have pointed first.
 
Share this answer
 
VB
<%@ Page Title="" Language="C#" 
EnableEventValidation="false" %>

change EnableEventValidation="false" in page
 
Share this answer
 
Comments
jithesh a 7-Nov-14 2:14am    
Thanks ;)
Hi,


I guess Set Command Name and Handle row Command Event to process

your Grid View as

XML
<asp:GridView ID="GridImport" runat="server" onrowcommand="GridView1_RowCommand" > 


Your Image Button as

<asp:ImageButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 24px; height: 24px;" ImageUrl="~/Images/download.png" CommandName="DownloadFile" />



then Row Command Event

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DownloadFile")
            {
                string filePath = (sender as ImageButton).CommandArgument;
                Response.ContentType = ContentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
                Response.WriteFile(filePath);
                Response.End();
            }
        }



Thanks

Siva Rm K
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900