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.
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.
You can also perform validation over the file input as shown in the demo code:
JavaScript
<telerik:RadCodeBlockID="RadCodeBlock1" runat="server">//On insert and update buttons click temporarily disables ajax to perform upload actions functionconditionalPostback(e, sender){var theRegexp =newRegExp("\.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;}}}functionvalidateRadUpload(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>
ASP.NET
<telerik:RadAjaxPanelID="RadAjaxPanel1"runat="server"ClientEvents-OnRequestStart="conditionalPostback"><telerik:RadProgressManagerID="RadProgressManager1"runat="server"/><telerik:RadProgressAreaRenderMode="Lightweight"ID="RadProgressArea1"runat="server"/><telerik:RadGridRenderMode="Lightweight"runat="server"ID="RadGrid1"AllowPaging="True"AllowSorting="True"AutoGenerateColumns="False"Width="97%"DataSourceID="SqlDataSource1"AllowAutomaticInserts="true"AllowAutomaticUpdates="true"AllowAutomaticDeletes="true"ShowStatusBar="True"GridLines="None"OnItemDataBound="RadGrid1_ItemDataBound"OnItemCreated="RadGrid1_ItemCreated"PageSize="3"><PagerStyleMode="NumericPages"AlwaysVisible="true"/><MasterTableViewWidth="100%"CommandItemDisplay="Top"DataKeyNames="ID"DataSourceID="SqlDataSource1"><Columns><telerik:GridEditCommandColumnButtonType="ImageButton"><HeaderStyleWidth="3%"/></telerik:GridEditCommandColumn><telerik:GridTemplateColumnHeaderText="Image Name"UniqueName="ImageName"SortExpression="Name"><ItemTemplate><asp:Labelrunat="server"ID="lblName"Text='<%#Eval("Name")%>'/></ItemTemplate><EditItemTemplate><telerik:RadTextBoxRenderMode="Lightweight"runat="server"Width="200px"ID="txbName"Text='<%#Bind("Name")%>'/><asp:RequiredFieldValidatorID="Requiredfieldvalidator1"runat="server"ControlToValidate="txbName"ErrorMessage="Please, enter a name!"Display="Dynamic"SetFocusOnError="true"/></EditItemTemplate><HeaderStyleWidth="30%"/></telerik:GridTemplateColumn><telerik:GridTemplateColumnHeaderText="Description"UniqueName="Description"DataField="Description"><ItemTemplate><asp:LabelID="lblDescription"runat="server"Text='<%#TrimDescription(CType(Eval("Description"),String))%>'/></ItemTemplate><EditItemTemplate><telerik:RadTextBoxRenderMode="Lightweight"ID="txbDescription"Width="300px"runat="server"TextMode="MultiLine"Text='<%#Bind("Description")%>'Height="150px"/></EditItemTemplate><ItemStyleVerticalAlign="Top"/></telerik:GridTemplateColumn><telerik:GridBinaryImageColumnDataField="Data"HeaderText="Image"UniqueName="Upload"ImageHeight="80px"ImageWidth="80px"ResizeMode="Fit"DataAlternateTextField="Description"DataAlternateTextFormatString="Image of {0}"><HeaderStyleWidth="10%"/></telerik:GridBinaryImageColumn><telerik:GridButtonColumnText="Delete"CommandName="Delete"ButtonType="ImageButton"><HeaderStyleWidth="2%"/></telerik:GridButtonColumn></Columns><EditFormSettings><EditColumnButtonType="ImageButton"/></EditFormSettings></MasterTableView></telerik:RadGrid></telerik:RadAjaxPanel><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:TelerikConnectionString %>"SelectCommand="SELECT * FROM [Images]"InsertCommand="INSERT INTO [Images] ([Name], [Description], [Data]) VALUES (@Name, @Description, @Data)"UpdateCommand="UPDATE [Images] SET [Name] = @Name, [Description] = @Description, [Data] = @Data WHERE [ID] = @ID"DeleteCommand="DELETE FROM [Images] WHERE [ID] = @ID"><InsertParameters><asp:ParameterName="Name"Type="String"/><asp:ParameterName="Description"Type="String"/><asp:ParameterName="Data"DbType="Binary"/></InsertParameters><UpdateParameters><asp:ParameterName="Name"Type="String"/><asp:ParameterName="Description"Type="String"/><asp:ParameterName="ID"Type="Int32"/><asp:ParameterName="Data"DbType="Binary"/></UpdateParameters><DeleteParameters><asp:ParameterName="ID"Type="Int32"/></DeleteParameters></asp:SqlDataSource>