Hi DevExpress Support team,
In our current project, we have an ASPxGrid with the purpose of providing downloads to our clients.
Putting it simply, it only has 3 visible columns, which are 'Description', 'FileType' and a templatecolumn with a button, named download.
What we would like to implement is, upon clicking the button, fire the button's click event and get the description and filetype of the current row in a string and download the file (which is on the root directory of the project).
After searching a bit, I found the current code for the download function:
string file = Server.MapPath("Report.pdf");
Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
How can we implement this? And, is there any better rotine to implement the download function?
Thanks in advance,
Your approach is good and I recommend that you rely on it when implementing this functionality in your application. I can suggest two possible scenarios:
Put an asp:Button in the column's ItemTemplate, specify its CommandName and CommandArgument parameters and handle the ASPxGrid.ItemCommand to react to button clicks. If you follow this way, please keep the following important rules in mind:
a) If you wish to work with templates, please note that in this mode only the items like the Group Panel, column headers, the summary footer are created on the server. Row items are created on the client. Therefore, you cannot access the controls inside templates in the server-side code. If you need to do this in the server-side code, please switch your Grid to Server mode.
b) Make sure to call the DataBind method within the page's Load event handler. When using templates, it is necessary in order to allow the ASPxGrid to properly build the hierarchy of its internal components. Otherwise, it will not be able to access controls in templates and to fire the corresponding events.
Put an ASPxButton in the column's ItemTemplate, set its AutoPostBack property to False and handle its Click client-side event. Within the event handler, call the window.open method to open another page which should load the PDF document.
Will these solutions serve your needs?
Hi Serge and thanks for your quick reply.
Well, the server side approach is the one that interests me more at the moment, but, I'd like to have your opinion on what would be the quickest approach.
Also, if you could, I'd like to request a sample on how to handle the button's quick event inside the itemtemplate and how to get the cell values of the current cell where the button was clicked.
I'm still very unexperienced with programming, hence why I'm asking for this if it's not a bigger burden for you.
The files to download won't be only PDF files, it was just an example.
And if possible, what's the code to implement a view option for files such as docx, pdf, etc?
Thanks in advance,
As for me, I prefer the client-side approach, because it allows your end-users to easily review the opened documents in a separate window. In this situation, you can get the necessary information (e.g. the document ID) from the corresponding data row and pass it to the secondary page via query fields.
If you wish, I will create a quick sample illustrating this idea.
Hi again Serge and thanks for your response.
Yes, if you wouldn't mind I would like to have a sample on how to do this function (but also on how to provide the download function) as I am clueless on what to do (once again, my very little experience in programming tends to be problematic).
Thanks in advance,
It's been a while and I'm sorry for taking so long to give you some feedback.
The project I was working on had to be dropped for a while, hence why I didn't have much of a chance to try out your sample and give you a reply. After a conversation with my project leader, he decided that the approached to be followed would be the server-side approach.
What I would like to know is, how can I get a cell's value upon clicking the Download button?
The download button is an ASPxButton inside the ASPxGrid's TemplateColumn's ItemTemplate.
You can do this via a binding expression, as illustrated in my sample:
<dxwg:TemplateColumn VisibleIndex="2"> <ItemTemplate> <input type="button" title="Show Details in a New Page" id="Details" value="Show Details..." onclick="window.open('PageDetail.aspx/?id=<%# DataBinder.Eval(Container.DataItem, "CategoryID")%>')" /> </ItemTemplate> </dxwg:TemplateColumn>
Thanks for your reply.
Well, what I was looking for was the server side approach, my team leader had completely dropped the client side approach from the table.
I found out the solution to this, I'm posting here the code should someone else stumble upon the same issue.
protected void ASPxButton1_Click(object sender, EventArgs e)
//Captures the File path from the clicked row
int index = int.Parse(ASPxGrid1.FocusedRow.Index.ToString());
string file = ASPxGrid1.Rows[index].DataControllerRow.GetOriginalValue("Filepath").ToString();
if (file != "")
//Downloads the specific File
Response.AddHeader("Content-Disposition" , "attachment; filename=" + file);
Thanks for your help!
I apologize for the misunderstanding, and thank you for posting your solution here. We greatly appreciate your valuable input. In the meantime, I recommend that you try handling the ASPxGrid.ItemCommand event to react to button clicks, as suggested in my first reply. This may be a more flexible solution if you decide to get rid of using a focused row in your Grid.