Dynamic user control(with javascript server code access) creation in ajax request error

6 posts, 1 answers
  1. Scott Shuppert
    Scott Shuppert avatar
    9 posts
    Member since:
    Feb 2010

    Posted 19 Jul 2010 Link to this post

    Hello.
    Sample code TestPage.aspx:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="TelerikDynamicUserControlJsApp.testPage" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div style="display:none">
                <rad:RadScriptManager runat="server" EnablePageMethods="true">
                    <Scripts>
                        <%--Needed for JavaScript IntelliSense in VS2010--%>
                        <%--For VS2008 replace RadScriptManager with ScriptManager--%>
                        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
                        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
                        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
                    </Scripts>
                </rad:RadScriptManager>
                <rad:RadAjaxManager runat="server">
                    <AjaxSettings>
                        <rad:AjaxSetting AjaxControlID="TestButton">
                            <UpdatedControls>
                                <rad:AjaxUpdatedControl ControlID="TestPlaceHolder" />
                            </UpdatedControls>
                        </rad:AjaxSetting>
                    </AjaxSettings>
                </rad:RadAjaxManager>
            </div>
            <div>
                <asp:Button ID="TestButton" runat="server" Text="Test" Width="60" OnClick="TestButton_OnClick" />
                <asp:PlaceHolder ID="TestPlaceHolder" runat="server"/>   
            </div>
        </form>
    </body>
    </html>

    protected void TestButton_OnClick(object sender, EventArgs e)
    {
       TestPlaceHolder.Controls.Add(LoadControl("TestUserControl.ascx"));
    }

    Sample code TestUserControl.ascx:
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TelerikDynamicUserControlJsApp.TestUserControl" %>
     
    <rad:RadScriptBlock>
        <script language="text/javascript">
            function GridCreated() {
                alert("<%= Grid.ClientID %>");
            }
        </script>
    </rad:RadScriptBlock>
     
    <div style="display:none">
        <rad:RadAjaxManagerProxy runat="server">
            <AjaxSettings>
                <rad:AjaxSetting AjaxControlID="Grid">
                    <UpdatedControls>
                        <rad:AjaxUpdatedControl ControlID="Grid"/>
                    </UpdatedControls>
                </rad:AjaxSetting>
            </AjaxSettings>
        </rad:RadAjaxManagerProxy>
    </div>
     
    <rad:RadGrid ID="Grid" runat="server" AllowSorting="true">
        <ClientSettings>
            <ClientEvents OnGridCreated="GridCreated" />
        </ClientSettings>
    </rad:RadGrid>

    public class TestData
    {
        public string TestField { get; set; }
    }
     
    protected void Page_Load(object sender, EventArgs e)
    {
        Grid.DataSource = new TestData[] { new TestData { TestField = "Test" } };
    }

    Code throws "Sys.WebForms.PageRequestManagerServerErrorException: Please, see whether wrapping the code block, generating the exception, within RadCodeBlock resolves the error". I need create usercontrol in ajaxrequest, usercontrol has javascriptcode with server code(<%%>). How resolve this error?
  2. Mira
    Admin
    Mira avatar
    1124 posts

    Posted 21 Jul 2010 Link to this post

    Hello Scott,

    Please take a look at the Loading user controls dynamically demo to see how the desired functionality can be achieved.

    I hope this helps.

    Greetings,
    Mira
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Scott Shuppert
    Scott Shuppert avatar
    9 posts
    Member since:
    Feb 2010

    Posted 21 Jul 2010 Link to this post

    It's not problem to dynamicaly load user control in ajax request, error apears when user control use javascript code with server code blocks(<%%>) it's showed and described in my sample. In demo usercontrols are not using javascript code.
  4. Answer
    Mira
    Admin
    Mira avatar
    1124 posts

    Posted 22 Jul 2010 Link to this post

    Hello Scott,

    I tried to replicate the issue which you described, but to no avail.
    Attached to this message, you will find the code which I used for testing.

    Please, take a look at it and let me know if there are any differences at your end, which I may be leaving out.

    Kind regards,
    Mira
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. test
    test avatar
    17 posts
    Member since:
    May 2007

    Posted 28 Jul 2010 Link to this post

    Hi I have the same problem when loading control dynamically.
    I have looked at Telerik Ajax forums. There are lots of discussions about this issue. I am just feeling strange and disappointed that the telerik people pretend not knowing the problem, and keep giving their dynamically loading sample (Loading user controls dynamically) which has nothing to do with the issue. The issue is simple; if you look at their sample

    they have three controls (login, task and search). If you are in the second control - task control (after login), do a view source from your right mouse. The HTML still shows the code for the login control (first control). Therefore there is no way your JavaScript function can work even if you load the script from the server (their suggestion) because your JavaScript function needs to access your control variables from the client side. They simply cannot find them.

    I wish the telerik people give a clear answer. But I doubt they know how!

    William

  6. Mira
    Admin
    Mira avatar
    1124 posts

    Posted 03 Aug 2010 Link to this post

    Hello William,

    Please take a look at the RADAjax Manager does not load JS files in dynamically loaded user control forum topic in which there is an detailed explanation of the issue and a lot of working samples implementing the desired functionality.

    I hope this helps.

    Best wishes,
    Mira
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top