Dynamic load problem!

8 posts, 0 answers
  1. Shukhrat Nekbaev
    Shukhrat Nekbaev avatar
    73 posts
    Member since:
    Oct 2010

    Posted 02 Aug 2011 Link to this post

    Hi,

    this one is a bit urgent, I need to implement it asap.
    In my application I have several update panels and everything is loaded dynamically (parent controls load child controls etc).
    I'm having an issue loading a control that has ImageEditor inside, I get:

    Object reference not set to an instance of an object. at System.Web.UI.AsyncPostBackTrigger.HasTriggered() at System.Web.UI.UpdatePanelTriggerCollection.HasTriggered() at System.Web.UI.PageRequestManager.ProcessUpdatePanels() at System.Web.UI.PageRequestManager.RenderPageCallback(HtmlTextWriter writer, Control pageControl) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Page.Render(HtmlTextWriter writer) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    Please create a new VS 2010 Rad controls project (.NET 4.0). Next create 2 user controls: MainContainer and PageImageEditor

    MainContainer is:
    <asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
           <ContentTemplate>
               <asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
               <asp:Button ID="btn_TestLoad" runat="server" Text="Test load" onclick="btn_TestLoad_Click" />
     
           </ContentTemplate>
       </asp:UpdatePanel>
    and cs file:

    private void LoadOwnControl()
            {
                Control ctrl = LoadControl(CurrentLoadedControlName);
                ph.Controls.Clear();
                ph.Controls.Add(ctrl);
            }
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
     
                if (Session["CurrentLoadedControlName"] != null)
                    LoadOwnControl();
            }
     
            protected void Page_Load(object sender, EventArgs e)
            {
                 
            }
     
            private string CurrentLoadedControlName
            {
                get
                {
                    if (Session["CurrentLoadedControlName"] == null)
                        Session["CurrentLoadedControlName"] = "~/DummyControl.ascx";
     
                    return Session["CurrentLoadedControlName"] as string;
                }
                set { Session["CurrentLoadedControlName"] = value; }
            }
     
            protected void btn_TestLoad_Click(object sender, EventArgs e)
            {
                CurrentLoadedControlName = "~/PageImageEditor.ascx";
                LoadOwnControl();
            }

    PageImageEditor is just:
    <telerik:RadImageEditor ID="RadImageEditor1" runat="server">
    </telerik:RadImageEditor>

    Drop MainContainer user control in Designer to Default.aspx:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>
     
    <%@ Register src="PageImageEditor.ascx" tagname="PageImageEditor" tagprefix="uc1" %>
     
    <%@ Register src="MainContainer.ascx" tagname="MainContainer" tagprefix="uc2" %>
     
    <!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>
        <%--<telerik:RadStyleSheetManager ID="RadStyleSheetManager2" runat="server">
            <StyleSheets>
                <telerik:StyleSheetReference Name="Telerik.Web.UI.Skins.ImageEditor.css" Assembly="Telerik.Web.UI" />
                <telerik:StyleSheetReference Name="Telerik.Web.UI.Skins.Default.ImageEditor.Default.css" Assembly="Telerik.Web.UI" />
                <telerik:StyleSheetReference Name="Telerik.Web.UI.Skins.ToolBar.css" Assembly="Telerik.Web.UI" />
                <telerik:StyleSheetReference Name="Telerik.Web.UI.Skins.Default.ToolBar.Default.css" Assembly="Telerik.Web.UI" />
                <telerik:StyleSheetReference Name="Telerik.Web.UI.Skins.Dock.css" Assembly="Telerik.Web.UI" />
                <telerik:StyleSheetReference Name="Telerik.Web.UI.Skins.Default.Dock.Default.css" Assembly="Telerik.Web.UI" />
            </StyleSheets>
        </telerik:RadStyleSheetManager> --%>
    </head>
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            <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>
        </telerik:RadScriptManager>
        <script type="text/javascript">
            //Put your JavaScript code here.
        </script>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        </telerik:RadAjaxManager>
        <div>
     
            <uc2:MainContainer ID="MainContainer1" runat="server" />
     
        </div>
        </form>
    </body>
    </html>

    start the project, click the button and you will instantly get error:
    Error: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.

    also give a try and comment out UpdatePanel code (not placeholder and button) in MainContainer, you will not get this error, but dialogs of ImageEditor won't load anything except their title (not sure if this is the only thing that doesn't work, didn't check, cause it didn't make sense to proceed further).

    So question is basically how to make this work in scenario described when control in inside UpdatePanel.

    Thank you!

    P.S.: in current project similar technique is applied to several other Telerik controls, no similar (major) problems faced so far.
  2. Shukhrat Nekbaev
    Shukhrat Nekbaev avatar
    73 posts
    Member since:
    Oct 2010

    Posted 02 Aug 2011 Link to this post

    Hi,

    Awhile I will play with iframe(create separate page, put ImageEditor there and load that page in iframe src's which in turn is located in dynamically loaded control) solution to that problem. Though would love to stick to approach mentioned above :) Let's see how it goes :)
    Thanks again!
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Niko
    Admin
    Niko avatar
    387 posts

    Posted 03 Aug 2011 Link to this post

    Hello Shukhrat,

    I was indeed able to reproduce the issue on my side. This is a peculiar case, where the ImageEditor cannot be added dynamically as late in the page life-cycle as the button OnClick event handler. Loading the ImageEditor in the OnInit handler works just fine. For that reason you implement a workaround for loading the user web control in the OnInit handler:
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
      
        if(IsPostBack)
            Session["CurrentLoadedControlName"] = "~/ImageEditor/ImageEditorInUpdatePanel/WebUserControl2.ascx";
      
        if (Session["CurrentLoadedControlName"] != null)
            LoadOwnControl();
    }

    Still we will investigate what is causing the issue you have reported and will try to come up with a solution in due time. You can track the progress on the fix through the following url - http://www.telerik.com/support/pits.aspx#/public/aspnet-ajax/7232.

    As a sign of gratitude for your persistence and efforts, I have updated your Telerik points.

    Best wishes,
    Niko
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  5. Shukhrat Nekbaev
    Shukhrat Nekbaev avatar
    73 posts
    Member since:
    Oct 2010

    Posted 03 Aug 2011 Link to this post

    Hi,

    Thank you for reply. In my case I do need to be able to load as I mentioned, cause application loads corresponding user control as a response to user actions, totally ajaxified, so I can't stick to static load of certain control. As I mentioned ealier I'm not experiencing major issues with other Telerik controls, but this. Also I've created a support ticket for that, cause I'm in a bit hurry.

    Thanks again for understanding and have a good day!
  6. Niko
    Admin
    Niko avatar
    387 posts

    Posted 04 Aug 2011 Link to this post

    Hi Shukhrat,

    I understand the difficulties that you are experiencing with this issue. However we will need more time to completely investigate the problem and decide on the best solution to have it fixed.
    Nevertheless you can still implement your logic based on the HTTP parameters. Here is a sample implementation relying on the fact that only buttons would decide on the user control to show:
    private void ChooseWebUserControlByKey()
    {
        var keys = Page.Request.Params.AllKeys;
      
        if(keys.Contains(btn_TestLoad.UniqueID))
            CurrentLoadedControlName = "~/ImageEditor/ImageEditorInUpdatePanel/WebUserControl2.ascx";
        else
            CurrentLoadedControlName = "~/DummyControl.ascx";
    }

    If you need to have a more general solution to capturing the postback control you can use the following helper method:
    public static class PageHelper
    {
        public static Control GetPostBackControl(Page page)
        {
            Control control = null;
      
            string ctrlname = page.Request.Params.Get("__EVENTTARGET");
            if (String.IsNullOrEmpty(ctrlname))
            {
                foreach (string ctl in page.Request.Form)
                {
                    Control c = page.FindControl(ctl);
                    if (c is System.Web.UI.WebControls.Button)
                    {
                        control = c;
                        break;
                    }
                }
            }
            else
            {
                control = page.FindControl(ctrlname);
            }
            return control;
        }
    }
    And implement a more general decision method:
    private void ChooseWebUserControl()
    {
        var postBackControl = PageHelper.GetPostBackControl(Page);
      
        switch (postBackControl.ID)
        {
            case "btn_TestLoad":
                CurrentLoadedControlName = "~/ImageEditor/ImageEditorInUpdatePanel/WebUserControl2.ascx";
                break;
            case "btn_TestInit":
                CurrentLoadedControlName = "~/ImageEditor/ImageEditorInUpdatePanel/WebUserControl2.ascx";
                break;
            default:
                CurrentLoadedControlName = "";
                break;
        }
    }

    Hope this helps.

    All the best,
    Niko
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  7. Shukhrat Nekbaev
    Shukhrat Nekbaev avatar
    73 posts
    Member since:
    Oct 2010

    Posted 04 Aug 2011 Link to this post

    Hi, thanks for reply.

    I have to mention that in my application I load controls form Page_Load event inside master control.
    Now back to our sandbox, approach above - I do similar to TreeView :) but, lets say code above works (and it does).
    Add UpdatePanel around placeholder control in MainContainer and run it, you will be able to load the image editor, but it will be behaving weird, try clicking on any toolbar items with dialogs, those wont load anything except header, close them, open again and you will get some other js related error. I tried to load in OnInit and in Page_Load of MainContainer - same result.

    Now set UpdateMode of the UpdatePanel in MainContainer to Condition. Control behaves nicely when loaded in OnInit and in Page_Load of MainContainer.

    Next, add UpdatePanel to Default.aspx that wraps MainContainer control itself, try loading image editor in Page_Load of MainContainer - error (no matter of UpdateMode setting for UpdatePanel inside Default.aspx).

    Next, same as above, but load image editor in Page_Load of MainContainer, UpdateMode for UpdatePanel inside Default.aspx - you will get weird behavior(try clicking on any toolbar items with dialogs, those wont load anything except header, close them, open again and you will get some other js related error)

    Same as last but set UpdateMode for UpdatePanel inside Default.aspx to Conditional. Control works ok, but somehow in most cases look minimized, like didn't expand to full height.

    When I say control works ok, I mean that toolsbar items can be opened at least 2 times, they fully load and dont cause js error no due load of control no due opening toolbar dialogs. Not aware of *real* workability in any cases.

    With that test cases I was trying to simulate a bit a structure that I have in main application and yes, it loads child controls in Page_Load event of master control (and it is fine with the rest RAD: TreeView, Grid, Button, TextBoxes, etc).

    Hope this helps to find solution!
    Thank you!
  8. Niko
    Admin
    Niko avatar
    387 posts

    Posted 09 Aug 2011 Link to this post

    Hi Shukhrat,

    The issue has been fixed. The fix will be available with the next internal build, which is due for the start of next week. Please, note that if the ImageEditor control is inside an UpdatePanel, the UpdateMode should be Conditional.

    Please, accept my apologies for the inconvenience caused.

    Greetings,
    Niko
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  9. Shukhrat Nekbaev
    Shukhrat Nekbaev avatar
    73 posts
    Member since:
    Oct 2010

    Posted 09 Aug 2011 Link to this post

    Hi,

    Great news! I'm looking forward next week's internal build!
    My personal regards for your outstanding support and sorry if I was putting a bit too much pressure, just really need it :)

    Will post my findings next week.
    Thanks again!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017