Not exactly accurate, but telerik won't let me edit the title.
The grid is a list of documents the user has stored. To view a stored document, I created the view link; within the ItemCommand event handler I create a file and stream it back to the user as shown below. However, the javascript to open a window to facilitate the streaming never gets invoked (except in IE browsers).
This is in a user control, although I don't think that's important. Here's the mark-up:
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"> <script type="text/javascript"> var mediaList = {}; mediaList.get_AddDocumentModalUrl = function mediaList_getAddDocumentModalUrl() { return '<%= Page.ResolveClientUrl("~/MediaLibrary/Modals/AddDocument.aspx") %>'; } mediaList.get_ViewDocumentModalUrl = function mediaList_getViewDocumentModalUrl(mimeType) { var url = '<%= Page.ResolveClientUrl("~/MediaLibrary/Modals/ViewDocument.aspx") %>' + '?type=' + mimeType; return url; } //On insert and update buttons click temporarily disables ajax to perform upload actions //for a more detailed description of why we need this. function conditionalPostback(sender, eventArgs) { var theRegexp = new RegExp("\.UpdateButton$|\.PerformInsertButton$", "ig"); if (eventArgs.get_eventTarget().match(theRegexp)) { var upload = $find(window['UploadId']); //AJAX is disabled only if file is selected for upload (i.e., new documents) if (upload != null && upload.getFileInputs()[0].value != "") { eventArgs.set_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; } } } </script></telerik:RadCodeBlock><telerik:RadScriptBlock ID="RadScriptBlock1" runat="server"> <script type="text/javascript"> mediaList.onAddDocumentWindowClosed = function mediaList.onAddDocumentWindowClosed(sender, eventArgs) { var returnValue = eventArgs.get_argument(); if (!Utils.WasRadWindowCancelled(returnValue, true)) { __doPostBack('', ''); } } mediaList.openAddDocumentWindow = function mediaList_openAddDocumentWindow() { Utils.OpenRadWindow ( mediaList.get_AddDocumentModalUrl(), 600, 400, mediaList.onAddDocumentWindowClosed ); } mediaList.openViewWindow = function mediaList_openViewWindow(mimeType) { document.getElementById("<%= streamFrame.ClientID %>").src = mediaList.get_ViewDocumentModalUrl(mimeType); } </script></telerik:RadScriptBlock><telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" ClientEvents-OnRequestStart="conditionalPostback"> <asp:Panel ID="pnlMsg" runat="server"> <asp:Label ID="lblMsg" runat="server" Style="color: Red; font-weight: bold;" /> </asp:Panel> <telerik:RadGrid ID="grdDocumentLibrary" runat="server" Skin="Default" AutoGenerateColumns="false" Width="100%" AllowPaging="true" PageSize="10" OnItemDataBound="grdDocumentLibrary_ItemDataBound" AllowAutomaticInserts="false" AllowAutomaticUpdates="false" OnEditCommand="grdDocumentLibrary_Edit" OnUpdateCommand="grdDocumentLibrary_Update" OnInsertCommand="grdDocumentLibrary_Insert" OnItemCommand="grdDocumentLibrary_ItemCommand" OnItemCreated="grdDocumentLibrary_ItemCreated" ShowStatusBar="true"> <PagerStyle Mode="NumericPages" AlwaysVisible="false" Position="Bottom" CssClass="Pager" /> <MasterTableView CommandItemDisplay="Top" ClientDataKeyNames="DocumentID" DataKeyNames="DocumentID"> <NoRecordsTemplate> <asp:Label Text="No documents to display" runat="server" /></NoRecordsTemplate> <Columns> <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditColumn"> </telerik:GridEditCommandColumn> <telerik:GridBoundColumn DataField="DocumentID" Visible="false" ReadOnly="true" UniqueName="DocID" /> <telerik:GridTemplateColumn HeaderText="Document Name" UniqueName="DocName" ReadOnly="false"> <ItemTemplate> <asp:Label ID="lblDocName" runat="server" Text='<%#Eval("Name") %>' /> </ItemTemplate> <EditItemTemplate> <telerik:RadUpload ID="ruDocument" runat="server" MaxFileInputsCount="1" /> <asp:CustomValidator ID="cvDocument" runat="server" ClientValidationFunction="validateRadUpload" ErrorMessage="Please select a file" Display="Dynamic" /> </EditItemTemplate> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn HeaderText="Description" UniqueName="Description"> <ItemTemplate> <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' /> </ItemTemplate> <EditItemTemplate> <telerik:RadTextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' Columns="50" Rows="5" TextMode="MultiLine" /> <des:RequiredTextValidator ID="rtvDescription" runat="server" ControlIDToEvaluate="txtDescription" ErrorMessage="* Required" /> </EditItemTemplate> </telerik:GridTemplateColumn> <telerik:GridBoundColumn HeaderText="Date Created" DataField="CreatedDate" ReadOnly="true" DataFormatString="{0:d}" /> <telerik:GridBoundColumn HeaderText="Operator" DataField="CreatedBy" ReadOnly="true" /> <telerik:GridImageColumn ImageUrl="~/Images/Tick.gif" HeaderText="Archived?" UniqueName="ArchivedColumn" /> <telerik:GridTemplateColumn ReadOnly="true" UniqueName="ViewColumn"> <ItemTemplate> <asp:LinkButton ID="lnkView" runat="server" Text="View" CommandArgument='<%#Eval("DocumentID") %>' CommandName="View" /> </ItemTemplate> </telerik:GridTemplateColumn> </Columns> <EditFormSettings> <EditColumn ButtonType="PushButton" /> </EditFormSettings> </MasterTableView> </telerik:RadGrid></telerik:RadAjaxPanel><telerik:RadAjaxLoadingPanel ID="loadingPanel" runat="server" Skin="Default" /><telerik:RadAjaxManagerProxy ID="ajaxProxy" runat="server"> <AjaxSettings> <telerik:AjaxSetting AjaxControlID="grdDocumentLibrary"> <UpdatedControls> <telerik:AjaxUpdatedControl ControlID="grdDocumentLibrary" LoadingPanelID="loadingPanel" /> </UpdatedControls> </telerik:AjaxSetting> </AjaxSettings></telerik:RadAjaxManagerProxy><iframe id="streamFrame" style="width: 1%; height: 1%" frameborder="0" scrolling="no" runat="server"></iframe>And the ItemCreated event handler, where I set up the "View" link.
protected void grdDocumentLibrary_ItemCreated(object sender, GridItemEventArgs e){ if (e.Item is GridDataItem) { LinkButton btn = (LinkButton)((GridDataItem)e.Item)["ViewColumn"].FindControl("lnkView"); Page page = HttpContext.Current.CurrentHandler as Page; Telerik.Web.UI.RadAjaxManager ajaxMgr = Telerik.Web.UI.RadAjaxManager.GetCurrent(page); if (ajaxMgr != null) { ajaxMgr.AjaxSettings.AddAjaxSetting(btn, grdDocumentLibrary, loadingPanel); } }}And here's where I handle the ItemCommand:
protected void grdDocumentLibrary_ItemCommand(object sender, GridCommandEventArgs e) { if (e.CommandName == RadGrid.InitInsertCommandName) { e.Canceled = true; e.Item.OwnerTableView.InsertItem(new PartnerDocument()); } else if (e.CommandName.Equals("View")) { BernardClientContext bcc = BuildClientContext(); DocumentService ds = new DocumentService(new DocumentRepository(bcc)); Document doc = ds.GetById(new Guid((string)e.CommandArgument)); string filePath = System.Configuration.ConfigurationManager.AppSettings["GeneratedDocuments"]; if (HttpContext.Current != null) { filePath = HttpContext.Current.Server.MapPath(filePath); } string fileName = null; Mutex fileMutex = null; try { while (true) { fileName = doc.FileName + "_" + System.DateTime.Now.ToString("ddMMyyyyhhmmss") + doc.DocumentType.Name; bool created; fileMutex = new Mutex(true, fileName.Replace("\\", "_"), out created); if (fileMutex != null && created) { if (!System.IO.File.Exists(filePath + fileName)) break; fileMutex.ReleaseMutex(); } System.Threading.Thread.Sleep(1000); } File.WriteAllBytes(filePath + fileName, doc.DocumentContent.Content); Page page = HttpContext.Current.CurrentHandler as Page; Telerik.Web.UI.RadAjaxManager ajaxMgr = Telerik.Web.UI.RadAjaxManager.GetCurrent(page); Session["documentToStream"] = filePath + fileName; if (ajaxMgr != null) { ajaxMgr.ResponseScripts.Add(String.Format("mediaList.openViewWindow('{0}'); return false;", doc.MimeType)); } else { page.ClientScript.RegisterStartupScript(page.GetType(), "documentStream", String.Format("mediaList.openViewWindow('{0}'); return false;", doc.MimeType), true); } } finally { if (fileMutex != null) fileMutex.ReleaseMutex(); } } }
Thanks,
Scott