This is a migrated thread and some comments may be shown as answers.

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

5 Answers 239 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Scott Shuppert
Top achievements
Rank 1
Scott Shuppert asked on 19 Jul 2010, 12:47 PM
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?

5 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 21 Jul 2010, 03:46 PM
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
0
Scott Shuppert
Top achievements
Rank 1
answered on 21 Jul 2010, 05:20 PM
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.
0
Accepted
Mira
Telerik team
answered on 22 Jul 2010, 03:39 PM
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
0
test
Top achievements
Rank 1
answered on 29 Jul 2010, 01:51 AM

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

0
Mira
Telerik team
answered on 03 Aug 2010, 09:08 AM
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
Tags
Ajax
Asked by
Scott Shuppert
Top achievements
Rank 1
Answers by
Mira
Telerik team
Scott Shuppert
Top achievements
Rank 1
test
Top achievements
Rank 1
Share this question
or