This example demonstrates how to create a templated column with buttons that are rendered as links with icons. When a user clicks a link, the corresponding file is downloaded.
Specify a column's DataItemTemplate property, add a button to the template, and set the button's RenderMode property to Link
.
<dx:GridViewDataColumn Caption="Doc" VisibleIndex="5">
<DataItemTemplate>
<dx:ASPxButton ID="ASPxButton1" runat="server" OnInit="ASPxButton1_Init"
AutoPostBack="False" RenderMode="Link" Text="Download">
<Image IconID="actions_download_16x16" />
</dx:ASPxButton>
</DataItemTemplate>
</dx:GridViewDataColumn>
Handle the button's server-side Init
event. In the handler, access the button's template container and get the container's key value. Then handle the button's client-side Click
event and pass the container's key value to the FileDownloadHandler.asxh
handler. In the handler, get the file specified by the container's key value and send this file to the browser.
protected void ASPxButton1_Init(object sender, EventArgs e) {
ASPxButton button = (ASPxButton)sender;
GridViewDataItemTemplateContainer container = (GridViewDataItemTemplateContainer)button.NamingContainer;
if (FileExists(container.KeyValue)) {
button.ClientSideEvents.Click = string.Format("function(s, e) {{ window.location = 'FileDownloadHandler.ashx?id={0}'; }}", container.KeyValue);
}
else {
button.ClientEnabled = false;
}
}
private bool FileExists(object key) {
return !string.IsNullOrEmpty(Product.GetData().First(p => p.ProductID.Equals(key)).ImagePath);
}
- Data.cs (VB: Data.vb)
- FileDownloadHandler.ashx.cs (VB: FileDownloadHandler.ashx.vb)
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)