Radgrid export in Custom Control
Hello
I created custom control to list some data from database. I used radgrid for this. Now everything works fine, except exporting.
When I click export button GvExpertise_ItemCommand is triggered and columns are set hidden. But after that exception is triggered:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
System.Web.UI.ControlCollection.Add(Control child) +9601391
Telerik.Sitefinity.Web.SitefinityRequiredControls.Page_PreRenderComplete(Object sender, EventArgs e) +521
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +121
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1155
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
CodeFile:
public class ExpertiseListView : SimpleView #region control info public static Guid GroupPageGuid = new Guid("6743bffb-7cce-4e5c-9d9e-fc7ff1fa5e73"); public static Guid PageGuid = new Guid("f0608080-5054-48b7-8218-a72ac6842e40"); public static string TemplateName = "ContactsFormModule.Resources.Backend.ExpertiseListView.ascx"; protected override string LayoutTemplateName get return TemplateName; #endregion #region controls protected virtual ITextControl LblTitle get return base.Container.GetControl<ITextControl>("LblTitle", true); protected virtual HyperLink BtnAdd get return base.Container.GetControl<HyperLink>("BtnAdd", true); protected virtual RadGrid GvExpertise get return base.Container.GetControl<RadGrid>("GvExpertise", true); #endregion public static string FieldName(bool isGeneral, bool isCompany) if (isGeneral && isCompany) return "..."; else if (isGeneral) return ".."; else if (isCompany) return "."; else return string.Empty; protected override void InitializeControls(GenericContainer container) LblTitle.Text = "Expertise"; LblAdd.Text = "Dodaj"; //BtnAdd.NavigateUrl = App.WorkWith().Page(ExpertiseEditView.PageGuid).Get().GetFullUrl(); GvExpertise.NeedDataSource += GvExpertise_NeedDataSource; GvExpertise.ItemCommand += GvExpertise_ItemCommand; GvExpertise.ItemDataBound += GvExpertise_ItemDataBound; void GvExpertise_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) GvExpertise.DataSource = new ContactsManager().Expertises; void GvExpertise_ItemDataBound(object sender, GridItemEventArgs e) if (e.Item is GridDataItem) ((LinkButton)((GridDataItem)e.Item)["DeleteColumn2"].Controls[0]).Text = ((Expertise)e.Item.DataItem).IsVisible ? "skrij" : "prikaži"; ((Literal)((GridDataItem)e.Item)["Field"].Controls[0]).Text = FieldName(((Expertise)e.Item.DataItem).General,((Expertise)e.Item.DataItem).Company); void GvExpertise_ItemCommand(object sender, GridCommandEventArgs e) if (e.CommandName == "hide") int iId = (int)((GridDataItem)e.Item).GetDataKeyValue("Id"); ContactsManager dataContext = new ContactsManager(); Expertise expertise = dataContext.Expertises.FirstOrDefault(x => x.Id == iId); expertise.IsVisible = !expertise.IsVisible; dataContext.SaveChanges(); GvExpertise.DataSource = new ContactsManager().Expertises; this.GvExpertise.DataBind(); else if (e.CommandName == RadGrid.ExportToCsvCommandName || e.CommandName == RadGrid.ExportToExcelCommandName || e.CommandName == RadGrid.ExportToPdfCommandName) GvExpertise.MasterTableView.Columns[3].Visible = false; GvExpertise.MasterTableView.Columns[4].Visible = false; <div class="sfWorkArea"> <div class="sfMessage sfGridViewMessage"><asp:Literal id="LblMessage" runat="server"></asp:Literal></div> <div> <div class="RadGrid RadGrid_Sitefinity rgTopOffset" tabindex="0"> <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"> <AjaxSettings> <telerik:AjaxSetting AjaxControlID="RadGrid1"> <UpdatedControls> <telerik:AjaxUpdatedControl ControlID="GvExpertise" /> </UpdatedControls> </telerik:AjaxSetting> </AjaxSettings> </telerik:RadAjaxManager> <telerik:RadGrid ID="GvExpertise" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="0" Width="100%" DataKeyNames="Id" GridLines="None" PageSize="30" EnableModelValidation="True" Skin="Sitefinity"> <ExportSettings ExportOnlyData="True" IgnorePaging="True" OpenInNewWindow="True" HideStructureColumns="true"> <Excel Format="Biff"></Excel> <Csv ColumnDelimiter="Semicolon" RowDelimiter="NewLine" EncloseDataWithQuotes="true" FileExtension=".csv" /> </ExportSettings> <MasterTableView DataKeyNames="Id" CommandItemDisplay="Top"> <CommandItemSettings ShowAddNewRecordButton="false" ShowExportToExcelButton="true" ShowExportToCsvButton="true"/> <Columns> <telerik:GridBoundColumn DataField="Id" HeaderText="Id" SortExpression="Id" /> <telerik:GridBoundColumn DataField="Description" HeaderText="Strokovno znanje" SortExpression="Description" /> <telerik:GridTemplateColumn DataField="Company,General" SortExpression="Company,General" UniqueName="Field"><ItemTemplate><asp:Literal runat="server" ID="LblField" /></ItemTemplate></telerik:GridTemplateColumn> <telerik:GridHyperLinkColumn DataNavigateUrlFields="Id" /> <telerik:GridButtonColumn Text='' ButtonType="LinkButton" CommandName="hide" UniqueName="DeleteColumn2"><HeaderStyle Width="20px"></HeaderStyle></telerik:GridButtonColumn> </Columns> </MasterTableView> </telerik:RadGrid> </div> </div></div>Hi,
Thank you for contacting Telerik Support.
Do you have any javascript code except the one that you sent me? Could you please try wrapping your code in RadCodeBlock as depicted in the following article and let me know if the issue still persits:
http://www.telerik.com/help/aspnet-ajax/ajax-radscriptblock-radcodeblock.html
Looking forward for your reply.
All the best,
Veronica Milcheva
the Telerik team
There is no javascript, only thing that is stripped from included code is control declaration:
<%@ Control Language="C#" %>
<%@ Register TagPrefix="telerik" Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" %>
From .cs namespace and imports are stripped.
using System;using System.Configuration;using System.Data.SqlClient;using System.Linq;using System.Security.Cryptography;using System.Text;using System.Web;using ContactsFormModule.Data;using System.Text.RegularExpressions;using System.Collections.Generic;using Telerik.Sitefinity.Web.UI;using System.Web.UI.WebControls;using System.Web.UI;using Telerik.Sitefinity.Modules.Pages;using Telerik.Sitefinity;using Telerik.Sitefinity.Modules.Libraries;using Telerik.Web.UI;namespace ContactsFormModule.WebControls.BackendHello,
After further investigation unfortunately this seems as a problem in the RadAjaxManager described and solved here.
Please accept my apologies for the caused inconvenience and let me know if you have further questions.
All the best,
Veronica Milcheva
the Telerik team
Thank you for reply.
I tried this solution, but exception still exists. The only difference is that exception occurs in ajax call, and not in full page render:
Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). at Function.Error.create (http://localhost:53212/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl06_TSM&compress=0&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3asl%3ac9cbdec3-c810-4e87-846c-fb25a7c08002%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2012.3.1016.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3asl%3a6feb3d99-e3a3-41be-bef8-96d1d2a7da93%3a16e4e7cd%3af7645509%3a24ee1bba%3ae330518b%3a2003d0b8%3a1e771326%3ac8618e41%3a58366029:6:2736) at Sys.WebForms.PageRequestManager._createPageRequestManagerServerError (http://localhost:53212/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl06_TSM&compress=0&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3asl%3ac9cbdec3-c810-4e87-846c-fb25a7c08002%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2012.3.1016.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3asl%3a6feb3d99-e3a3-41be-bef8-96d1d2a7da93%3a16e4e7cd%3af7645509%3a24ee1bba%3ae330518b%3a2003d0b8%3a1e771326%3ac8618e41%3a58366029:15:11490) at Sys.WebForms.PageRequestManager._parseDelta (http://localhost:53212/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl06_TSM&compress=0&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3asl%3ac9cbdec3-c810-4e87-846c-fb25a7c08002%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2012.3.1016.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3asl%3a6feb3d99-e3a3-41be-bef8-96d1d2a7da93%3a16e4e7cd%3af7645509%3a24ee1bba%3ae330518b%3a2003d0b8%3a1e771326%3ac8618e41%3a58366029:15:29608) at Sys.WebForms.PageRequestManager._onFormSubmitCompleted (http://localhost:53212/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl06_TSM&compress=0&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3asl%3ac9cbdec3-c810-4e87-846c-fb25a7c08002%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2012.3.1016.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3asl%3a6feb3d99-e3a3-41be-bef8-96d1d2a7da93%3a16e4e7cd%3af7645509%3a24ee1bba%3ae330518b%3a2003d0b8%3a1e771326%3ac8618e41%3a58366029:15:25637) at Array.0 (http://localhost:53212/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl06_TSM&compress=0&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3asl%3ac9cbdec3-c810-4e87-846c-fb25a7c08002%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2012.3.1016.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3asl%3a6feb3d99-e3a3-41be-bef8-96d1d2a7da93%3a16e4e7cd%3af7645509%3a24ee1bba%3ae330518b%3a2003d0b8%3a1e771326%3ac8618e41%3a58366029:6:307) at http://localhost:53212/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl06_TSM&compress=0&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3asl%3ac9cbdec3-c810-4e87-846c-fb25a7c08002%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2012.3.1016.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3asl%3a6feb3d99-e3a3-41be-bef8-96d1d2a7da93%3a16e4e7cd%3af7645509%3a24ee1bba%3ae330518b%3a2003d0b8%3a1e771326%3ac8618e41%3a58366029:6:51370 at Sys.Net.WebRequest.completed (http://localhost:53212/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl06_TSM&compress=0&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3asl%3ac9cbdec3-c810-4e87-846c-fb25a7c08002%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2012.3.1016.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3asl%3a6feb3d99-e3a3-41be-bef8-96d1d2a7da93%3a16e4e7cd%3af7645509%3a24ee1bba%3ae330518b%3a2003d0b8%3a1e771326%3ac8618e41%3a58366029:6:89652) at XMLHttpRequest._onReadyStateChange [as onreadystatechange] (http://localhost:53212/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl06_TSM&compress=0&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3asl%3ac9cbdec3-c810-4e87-846c-fb25a7c08002%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2012.3.1016.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3asl%3a6feb3d99-e3a3-41be-bef8-96d1d2a7da93%3a16e4e7cd%3af7645509%3a24ee1bba%3ae330518b%3a2003d0b8%3a1e771326%3ac8618e41%3a58366029:6:84251)<asp:Label runat="server"> <asp:UpdatePanel runat="server" ID="PanelGvExpertise"> <ContentTemplate> <asp:Label runat="server"> <telerik:RadGrid ID="GvExpertise" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="0" Width="100%" DataKeyNames="Id" GridLines="None" PageSize="30" EnableModelValidation="True" Skin="Sitefinity"> <ExportSettings ExportOnlyData="True" IgnorePaging="True" OpenInNewWindow="True" HideStructureColumns="true"> <Excel Format="Biff"></Excel> <Csv ColumnDelimiter="Semicolon" RowDelimiter="NewLine" EncloseDataWithQuotes="true" FileExtension=".csv" /> </ExportSettings> <MasterTableView DataKeyNames="Id" CommandItemDisplay="Top"> <CommandItemSettings ShowAddNewRecordButton="false" ShowExportToExcelButton="true" ShowExportToCsvButton="true"/> <Columns> <telerik:GridBoundColumn DataField="Id" HeaderText="Id" SortExpression="Id" /> <telerik:GridBoundColumn DataField="Description" HeaderText="Strokovno znanje" SortExpression="Description" /> <telerik:GridTemplateColumn DataField="Company,General" SortExpression="Company,General" UniqueName="Field"><ItemTemplate><asp:Literal runat="server" ID="LblField" /></ItemTemplate></telerik:GridTemplateColumn> <telerik:GridHyperLinkColumn DataNavigateUrlFields="Id" /> <telerik:GridButtonColumn Text='' ButtonType="LinkButton" CommandName="hide" UniqueName="DeleteColumn2"><HeaderStyle Width="20px"></HeaderStyle></telerik:GridButtonColumn> </Columns> </MasterTableView> </telerik:RadGrid> </asp:Label> </ContentTemplate> </asp:UpdatePanel> </asp:Label>Hi,
Thank you for the code attached!
I cannot understand why did you added the UpdatePanel in your latest code? One possible solution to the problem shown in the forum post that I gave you to just wrap the RadGrid (control that is to be updated) with any server control. So wrapping the RadGrid with Label with "runat=server" attribute should work.
If this still does not helps could you please set the EnableAjax property of the RadAjaxManager to "false" in order to test whether the problem really stays in the RadAjaxManager. If you don't receive the error any more - could you please send me all your code (whole project if possible) or just this page where you are expriencing the issue in attachment so that I can inspect it and help you?
Thank you!
All the best,
Veronica Milcheva
the Telerik team
I read that RadAjaxManager put some dummy control before and after target control,
so I tryed to wrap RadGrid with <span runat="server"> , with no success
Then I tryed to wrap RadGrid with <asp:Label , with no success
Then I read that RadAjaxManager wrap radgrid(or any other control) into updatecontrol if control isn't update panel and this is where ajax error occures.
I'll prepare project soon
I tried enableajax with no success.
I uploaded whole project to www.sendspace.com/.../dfj77v
Login for sitefinity is admin/testtest
I'm using 5.2 sitefinity binaries(found at C:\Program Files (x86)\Telerik\Sitefinity 5.2\SDK\Content\Common\Dependencies).
I'm looking forward to your reply