New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Binary Images and Upload in Grid

This example illustrates the usage of built-in GridBinaryImageColumn and how to display and edit images in it. Each cell in a GridBinaryImageColumn contains an image streamed from a binary image source field (specified through the DataField property of the column). When used, this column will show a RadBinaryImage control in view mode and RadUpload in edit mode to upload an image.

The image will be sized automatically to ImageHeight and ImageWidth pixel values if the ResizeMode property of the column is different than None. Possible values for the ResizeMode property of the column are:

  • Crop (the image will be trimmed)

  • Fit (the image will be sized to fit the given dimensions)

  • None (default)

Additionally, you can set the DataAlternateTextField property to specify by which field in the grid source the column will be sorted/filtered. For the filtering you must also set explicitly the DataType property of the column to the type of the field specified through the DataAlternateTextField property (System.String in the common case). You can also apply format using the DataAlternateTextFormatString property.

You need to register the http handler of the RadBinaryImage control (which is part of built-in GridBinaryImageColumn) manually in the web.config file to ensure that it will be served as expected when the page is rendered.

XML
<httpHandlers>
    <remove path="*.asmx" verb="*" />
    <add path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" validate="false" />
</httpHandlers> |

If you want to set the GridBinaryImageColumn's image filename which will appear inside SaveAs browser dialog when image is saved, set the SavedImageName property of the column

Since RadUpload cannot upload files using AJAX calls (this is a limitation of the XmlHttpRequest component, used in all AJAX frameworks for asynchronous calls to the application), to upload a file perform a full page postback.

BinaryImage and Upload in RadGrid

You can also perform validation over the file input as shown in the demo code:

JavaScript
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
//On insert and update buttons click temporarily disables ajax to perform upload actions      
function conditionalPostback(e, sender) {
  var theRegexp = new RegExp("\.UpdateButton$|\.PerformInsertButton$", "ig");
  if (sender.EventTarget.match(theRegexp)) {
    var upload = $find(window['UploadId']);
    //AJAX is disabled only if file is selected for upload                  
    if (upload.getFileInputs()[0].value != "") {
      sender.EnableAjax = false;
    }
  }
}
function validateRadUpload(source, e) {
  e.IsValid = false;
  var upload = $find(source.parentNode.getElementsByTagName('div')[0].id);
  var inputs = upload.getFileInputs(); for (var i = 0; i < inputs.length; i++) {
    //check for empty string or invalid extension     
    if (inputs[i].value != "" && upload.isExtensionValid(inputs[i].value)) {
      e.IsValid = true;
      break;
    }
  }
}
</telerik:RadCodeBlock>
Not finding the help you need?
Contact Support