Hi,
I want to use a generated filename for exports. I want to generate the filename only when the user does an export (I use the RadGrid's built-in feature for exporting by showing the export to Excel/Pdf/Word buttons).
If I set the filename in CreateChildControls, it works fine.
But in which event can I determine if there is an export running and change the filename? I tried the GridExporting event but it seems too late.
I want to use a generated filename for exports. I want to generate the filename only when the user does an export (I use the RadGrid's built-in feature for exporting by showing the export to Excel/Pdf/Word buttons).
If I set the filename in CreateChildControls, it works fine.
But in which event can I determine if there is an export running and change the filename? I tried the GridExporting event but it seems too late.
5 Answers, 1 is accepted
0
Shinu
Top achievements
Rank 2
answered on 07 Jun 2012, 04:55 AM
Hi,
Try setting the FileName in the ItemCommand event as shown below.
C#:
Thanks,
Shinu.
Try setting the FileName in the ItemCommand event as shown below.
C#:
protected
void
Radgrid1_ItemCommand(
object
sender, GridCommandEventArgs e)
{
if
(e.CommandName == RadGrid.ExportToPdfCommandName)
{
Radgrid1.ExportSettings.FileName =
"yourfilename"
;
}
if
(e.CommandName == RadGrid.ExportToExcelCommandName)
{
Radgrid1.ExportSettings.FileName =
"yourfilename"
;
}
if
(e.CommandName == RadGrid.ExportToWordCommandName)
{
Radgrid1.ExportSettings.FileName =
"yourfilename"
;
}
}
Thanks,
Shinu.
Jeff
commented on 31 Jan 2023, 07:25 PM
Top achievements
Rank 1
Wouldn't you be able to set the file name outside of the if statements, rather than code it 3 times?
Lance | Senior Manager Technical Support
commented on 31 Jan 2023, 07:37 PM
Telerik team
Hi Jeff,
Yes, you can set it anywhere you want, the example from Shinu just omits the file extension. This could have also been written as:
if (e.CommandName == RadGrid.ExportToPdfCommandName)
{
Radgrid1.ExportSettings.FileName = "yourfilename.pdf";
}
if (e.CommandName == RadGrid.ExportToExcelCommandName)
{
Radgrid1.ExportSettings.FileName = "yourfilename.xlsx";
}
if (e.CommandName == RadGrid.ExportToWordCommandName)
{
Radgrid1.ExportSettings.FileName = "yourfilename.docx";
}
However, if we do as you state, it requires more lines of code, but decreases the risk of misspelling the file name
// Extra line 1
var fileName = "";
if (e.CommandName == RadGrid.ExportToPdfCommandName)
{
fileName = "yourfilename.pdf";
}
if (e.CommandName == RadGrid.ExportToExcelCommandName)
{
fileName = "yourfilename.xlsx";
}
if (e.CommandName == RadGrid.ExportToWordCommandName)
{
fileName = "yourfilename.docx";
}
// Extra line 2
Radgrid1.ExportSettings.FileName = fileName;
Ultimately, my suggestion would be to use a switch statement with a base filename
var nameOnly = "filename";
switch(e.CommandName)
{
case RadGrid.ExportToPdfCommandName:
Radgrid1.ExportSettings.FileName = nameOnly + ".pdf";
break;
case RadGrid.ExportToExcelCommandName:
Radgrid1.ExportSettings.FileName = nameOnly + ".xlsx";
break;
case RadGrid.ExportToWordCommandName:
Radgrid1.ExportSettings.FileName = nameOnly + ".docx";
break;
default:
break;
}
1
Dave
Top achievements
Rank 1
answered on 08 Sep 2015, 06:16 PM
This does not seem to work for me. I want to add the date and time of the export in the file name but changing the filename at this point does nothing. Anybody know if it's actually possible?
0
Hi Dave,
I've created a sample RadGrid web site to test the described behavior. Could you run the attached application and let me know about the result?
Regards,
Eyup
Telerik
I've created a sample RadGrid web site to test the described behavior. Could you run the attached application and let me know about the result?
Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans?
Do you want to know when a feature you care about is added or when a bug fixed?
Explore the
Telerik Feedback Portal
and vote to affect the priority of the items
0
Dave
Top achievements
Rank 1
answered on 11 Sep 2015, 04:43 PM
Thanks for the reply. This is pretty much exactly what we tried. Seeing that this should work pushed me to dig further. Our grids are created dynamically from code behind and we also use our own export button. Turns out I can set the export name at the same time our export button is created and this works and the datetime changes each time I click on the button.
0
Hi,
We are glad to hear that the example provided by my colleague Eyup helped to achieve your goal. Do not hesitate to contact us in case other questions arise.
Regards,
Pavlina
Telerik
We are glad to hear that the example provided by my colleague Eyup helped to achieve your goal. Do not hesitate to contact us in case other questions arise.
Regards,
Pavlina
Telerik
Do you want to have your say when we set our development plans?
Do you want to know when a feature you care about is added or when a bug fixed?
Explore the
Telerik Feedback Portal
and vote to affect the priority of the items