problém with dynamic add control and RadAjaxManager

2 posts, 0 answers
  1. Jan
    Jan avatar
    8 posts
    Member since:
    Mar 2012

    Posted 06 Mar 2012 Link to this post

    HI,
    I have page where I dynamic add control. When i add radgrid with RadAjaxManager and clikc on menu to get on ather page >> ERROR:

    Dont found control with ID ActiveLeftMenu$rgridQuestion for trigger UpdatePanel ActiveLeftMenu$rgridQuestionPanel.


    please help
    page for dynamic add controls
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LeftMenuAndContent.ascx.cs"
        Inherits="Elearning.Usecases.UserControl.LeftMenuAndContent" %>
     
    <div class="sheet">
        <div class="sheet-body">
            <div class="content-layout">
                <div class="content-layout-row">
                    <div class="layout-cell sidebar1">
                        <div class="vmenublock">
                            <div class="vmenublock-body">
                                <div class="vmenublockcontent">
                                    <div class="vmenublockcontent-body">
                                        <asp:Repeater ID="repLeftMenu" runat="server">
                                            <ItemTemplate>
                                                <li runat="server" id="ListItem">
                                                    <asp:LinkButton ID="lnkNavigate" runat="server" CausesValidation="false">
                                                        <span class="l"></span><span class="r"></span><span class="t">
                                                            <asp:Literal runat="server" ID="litText"></asp:Literal>
                                                        </span>
                                                    </asp:LinkButton>
                                                </li>
                                            </ItemTemplate>
                                            <HeaderTemplate>
                                                <ul class="vmenu">
                                            </HeaderTemplate>
                                            <FooterTemplate>
                                                </ul>
                                            </FooterTemplate>
                                        </asp:Repeater>
                                        <div class="cleared">
                                        </div>
                                    </div>
                                </div>
                                <div class="cleared">
                                </div>
                            </div>
                        </div>
                        <div class="cleared">
                        </div>
                    </div>
                    <div class="layout-cell content">
                        <div class="post">
                            <h1>
                                <asp:Literal runat="server" ID="litTitleContent"></asp:Literal>
                            </h1>
                                    <asp:PlaceHolder ID="plhContent" EnableViewState="False" runat="Server" />
                        </div>
                        <div class="cleared">
                        </div>
                    </div>
                </div>
            </div>
            <div class="cleared">
            </div>
            <div class="cleared">
            </div>
        </div>
    </div>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using CSCR.Hermes.Controllers;
    using Elearning.UserInterfaceLayer;
     
    namespace Elearning.Usecases.UserControl
    {
        public partial class LeftMenuAndContent : BaseUserControl
        {
            private Control contentControl;
            /// <summary>
            /// Get and set name of active usecase
            /// </summary>
            string ActiveLeftMenu
            {
                get { return (string)ViewState["ActiveLeftMenu"]; }
                set { ViewState["ActiveLeftMenu"] = value; }
                //get { return _activeLeftMenu; }
                //set { _activeLeftMenu = value; }
            }
     
            private string _activeLeftMenu = string.Empty;
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
     
                    var hermes = GetHermesControl();
                    if (hermes.Parameters != null && hermes.Parameters[ElearningCommon.PropertyKeys.LessonID] != null)
                        LessonId = Convert.ToInt32(hermes.Parameters[ElearningCommon.PropertyKeys.LessonID]);
                    if (hermes.Parameters != null && hermes.Parameters[ElearningCommon.PropertyKeys.ExamID] != null)
                        ExamID = Convert.ToInt32(hermes.Parameters[ElearningCommon.PropertyKeys.ExamID]);
                    if (hermes.Parameters != null && hermes.Parameters[ElearningCommon.PropertyKeys.ActiveItemLeftMenu] != null)
                        ActiveLeftMenu = hermes.Parameters[ElearningCommon.PropertyKeys.ActiveItemLeftMenu];
     
                    repLeftMenu.DataSource = GetMenuItems();
                    repLeftMenu.DataBind();
               //    if(!GetHermesControl().IsPostBack)
                    SetActiveMenuAndLoadContent();
            }
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                repLeftMenu.ItemCommand += OnItemCommand_repLeftMenu;
                repLeftMenu.ItemDataBound += OnItemDataBound_repLeftMenu;
            }
            public void SetActiveLeftMenu(string name)
            {
                ActiveLeftMenu = name;
            }
     
            private List<Tab> GetMenuItems()
            {
                var menuItems = new List<Tab>();
                switch (GetHermesControl().CurrentUsecase)
                {
                    case ElearningCommon.UCConstants.UCManagmentLesson:
                        menuItems.Add(new Tab() { Name = (string)GetLocalResourceObject("Detail"), UsecaseName = ElearningCommon.UCConstants.LessonDetail });
                        if (LessonId > 0)
                        {
                            menuItems.Add(new Tab() { Name = (string)GetLocalResourceObject("Users"), UsecaseName = ElearningCommon.UCConstants.LessonUsers });
                            menuItems.Add(new Tab() { Name = (string)GetLocalResourceObject("Tests"), UsecaseName = ElearningCommon.UCConstants.TestsList });
                        }
                        break;
                    case ElearningCommon.UCConstants.UCManagmentTest:
                        menuItems.Add(new Tab() { Name = (string)GetLocalResourceObject("Detail"), UsecaseName = ElearningCommon.UCConstants.ExamDetail });
                        if (ExamID > 0)
                            menuItems.Add(new Tab() { Name = (string)GetLocalResourceObject("QuestionAnswers"), UsecaseName = ElearningCommon.UCConstants.QuestionList });
                        break;
                }
                return menuItems;
            }
     
            private void SetActiveMenuAndLoadContent()
            {
                SetActiveMenu();
                LoadContent();
            }
     
            protected void OnItemDataBound_repLeftMenu(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    var dataItem = (Tab)e.Item.DataItem;
                    var lb = e.Item.FindControl("lnkNavigate") as LinkButton;
                    var lit = e.Item.FindControl("litText") as Literal;
                    lit.Text = dataItem.Name;
                    lb.CommandName = dataItem.UsecaseName;
                    if (string.IsNullOrEmpty(ActiveLeftMenu))
                        ActiveLeftMenu = dataItem.UsecaseName;
                }
            }
     
            protected void OnItemCommand_repLeftMenu(object source, RepeaterCommandEventArgs e)
            {
             //   if (ActiveLeftMenu == e.CommandName)
              //      return;
                ActiveLeftMenu = e.CommandName;
                SetActiveMenuAndLoadContent();
            }
            protected override void LoadViewState(object savedState)
            {
                base.LoadViewState(savedState);
               // if (!string.IsNullOrEmpty(ActiveLeftMenu))
                //    SetActiveMenuAndLoadContent();
            }
            private void LoadContent()
            {
                litTitleContent.Text = (string)GetLocalResourceObject(ActiveLeftMenu);
                contentControl =
                    Page.LoadControl(
                        GetHermesControl().GetAppItemRelativeUrl(string.Format("~/Usecases/UserControl/{0}.ascx", ActiveLeftMenu)));
                plhContent.Controls.Clear();
                contentControl.ID = "ActiveLeftMenu";
                if (contentControl is BaseUserControl)
                {
                    (contentControl as BaseUserControl).LessonId = LessonId;
                    (contentControl as BaseUserControl).ExamID = ExamID;
                }
                plhContent.Controls.Add(contentControl);
                contentControl.DataBind();
     
            }
     
            private void SetActiveMenu()
            {
                for (int i = 0; i < repLeftMenu.Items.Count; i++)
                {
                    HtmlGenericControl listItem = (HtmlGenericControl)repLeftMenu.Items[i].FindControl("ListItem");
                    LinkButton listItemLink = (LinkButton)listItem.FindControl("lnkNavigate");
                    if (listItemLink != null)
                        listItemLink.Attributes.Add("class", listItemLink.CommandName == ActiveLeftMenu ? "active" : "");
                }
            }
        }
    }

    and here control with radgrid 
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UCQuestionDetail.ascx.cs"
        Inherits="Elearning.Usecases.UserControl.QuestionDetail" %>
    <%@ Import Namespace="Elearning" %>
    <%@ Register TagPrefix="include" TagName="imgbutton_1" Src="~/Usecases/CustomField/ButtonWithImage.ascx" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <%@ Register TagPrefix="include" TagName="TopMenu" Src="~/Usecases/UserControl/TopMenu.ascx" %>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" >
        <ajaxsettings>
                <telerik:AjaxSetting AjaxControlID="rgridAnswers">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl  ControlID="rgridAnswers" LoadingPanelID="RadAjaxLoadingPanel1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </ajaxsettings>
    </telerik:RadAjaxManager>
     <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server"  />
    <include:TopMenu ID="TopMenu1" runat="server"></include:TopMenu>
    <div class="sheet">
        <div class="sheet-body">
            <div class="content-layout">
                <div class="content-layout-row">
                    <div class="layout-cell content">
                        <div class="post">
                            <h1>
                                <asp:Literal runat="server" ID="litTitleContent" meta:resourcekey="title"></asp:Literal>
                            </h1>
                            <div>
                           
                                <div style="float: right; width: 55%">
                                    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server"
                                        HorizontalAlign="NotSet" >
                                        <telerik:RadGrid ID="rgridAnswers" runat="server" GridLines="None" OnRowDrop="rgridAnswers_OnRowDrop"
                                            AutoGenerateColumns="False" ShowStatusBar="True" AllowAutomaticInserts="True"
                                            OnNeedDataSource="rgridAnswers_NeedDataSource" OnItemCreated="OnItemCreated"
                                            OnDeleteCommand="rgridAnswers_DeleteCommand" CellSpacing="0" >
                                            <ClientSettings AllowRowsDragDrop="True">
                                                <selecting allowrowselect="True" enabledragtoselectrows="False" />
                                              
                                            </ClientSettings>
                                            <mastertableview commanditemdisplay="Top" datakeynames="ID" width="100%" >
                                                <CommandItemSettings  AddNewRecordText='<%$ Resources:AddNewRecordText %>' RefreshText='<%$ Resources:RefreshText %>' />
                                                <Columns>
                                                    <telerik:GridEditCommandColumn ButtonType="ImageButton"
                                                        FilterControlAltText="Filter EditCommandColumn column" >
                                                        <ItemStyle CssClass="MyImageButton" />
                                                    </telerik:GridEditCommandColumn>
                                                    <telerik:GridBoundColumn DataField="Text"
                                                        FilterControlAltText="Filter Text column"
                                                        meta:resourcekey="QuestionText" UniqueName="Text">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="Description"
                                                        FilterControlAltText="Filter Description column" 
                                                        meta:resourcekey="QuestionDescription" UniqueName="Description">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="ScorePerc"
                                                        FilterControlAltText="Filter ScorePerc column" 
                                                        meta:resourcekey="QuestionScorePerc" UniqueName="ScorePerc">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn column"
                                                        meta:resourcekey="QuestionIsValid"
                                                        UniqueName="TemplateColumn">
                                                        <ItemTemplate>
                                                            <asp:ImageButton ID="imgIsValid" runat="server"
                                                                ImageUrl='<%# (bool)Eval("IsValid")? GetAppItemRelativeUrl(ElearningCommon.Icons.Public): GetAppItemRelativeUrl(ElearningCommon.Icons.Delete) %>'
                                                                meta:resourcekey="imgIsValidResource1" />
                                                        </ItemTemplate>
                                                        <HeaderStyle HorizontalAlign="Center" />
                                                        <ItemStyle HorizontalAlign="Center" />
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn1 column"
                                                         UniqueName="TemplateColumn1">
                                                        <ItemTemplate>
                                                            <asp:HiddenField ID="hfSortOrder" runat="server"
                                                                Value='<%# Eval("SortOrder") %>' />
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete"
                                                        ConfirmDialogType="RadWindow" 
                                                        ConfirmTitle="Delete" FilterControlAltText="Filter DeleteColumn column"
                                                        meta:resourcekey="QuestionReallyDelete" Text="Delete"
                                                        UniqueName="DeleteColumn">
                                                        <ItemStyle CssClass="MyImageButton" HorizontalAlign="Center" />
                                                    </telerik:GridButtonColumn>
                                                </Columns>
                                                <editformsettings editformtype="WebUserControl"
                                                    usercontrolname="~/Usecases/UserControl/AnswersDetail.ascx">
                                                    <editcolumn filtercontrolalttext="Filter EditCommandColumn1 column"
                                                        uniquename="EditCommandColumn1">
                                                    </editcolumn>
                                                </editformsettings>
                                            </mastertableview>
                                            <filtermenu enableimagesprites="False">
                                            </filtermenu>
                                        </telerik:RadGrid>
                                    </telerik:RadAjaxPanel>
                                </div>
                            </div>
                            <div class="cleared">
                            </div>
                           
                        </div>
                    </div>
                </div>
            </div>
            <div class="cleared">
            </div>
            <div class="cleared">
            </div>
        </div>
    </div>


    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using CSCR.Hermes.Controllers;
    using Elearning.DataAccessLayer;
    using Telerik.Web.UI;
     
    namespace Elearning.Usecases.UserControl
    {
        public partial class QuestionDetail : HermesUserControl
        {
       
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                if (Parameters != null && Parameters[ElearningCommon.PropertyKeys.LessonID] != null)
                    LessonId = Convert.ToInt32(Parameters[ElearningCommon.PropertyKeys.LessonID]);
                if (Parameters != null && Parameters[ElearningCommon.PropertyKeys.ExamID] != null)
                    ExamID = Convert.ToInt32(Parameters[ElearningCommon.PropertyKeys.ExamID]);
                if (Parameters[ElearningCommon.PropertyKeys.QuestionID] != null)
                    QuestionId = int.Parse(Parameters[ElearningCommon.PropertyKeys.QuestionID]);
          
                TopMenu1.DataBind();
              }
     
            public int QuestionId
            {
                get
                {
                    if (ViewState[ElearningCommon.PropertyKeys.QuestionID] == null)
                        return -1;
                    return (int)ViewState[ElearningCommon.PropertyKeys.QuestionID];
                }
                set { ViewState[ElearningCommon.PropertyKeys.QuestionID] = value; }
            }
       
     
            protected void rgridAnswers_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                rgridAnswers.DataSource = DataManager.Instance.GetAnswersByQuestion(QuestionId);
            }
     
         
            protected void rgridAnswers_DeleteCommand(object sender, GridCommandEventArgs e)
            {
                string ID = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();
                DataManager.Instance.DeleteAnswer(int.Parse(ID));
            }
     
            private void rgridAnswers_Update(object sender, CommandEventArgs e,Answer answer)
            {
                rgridAnswers.MasterTableView.ClearEditItems();
                DataManager.Instance.UpdateAnswer(answer);
                rgridAnswers.Rebind();
            }
     
            private void rgridAnswers_Insert(object sender, CommandEventArgs e,Answer answer)
            {
                answer.QuestionReference.SetId(EntityHelper.References.Questions,QuestionId);
                if (rgridAnswers.Items.Count > 0)
                {
                    var order = (HiddenField)rgridAnswers.Items[rgridAnswers.Items.Count - 1].FindControl("hfSortOrder");
                    answer.SortOrder = int.Parse(order.Value) + 1;
                }
                else
                    answer.SortOrder = 1;
                DataManager.Instance.InsertAnswer(answer);
                rgridAnswers.Rebind();
            }
     
            protected void OnItemCreated(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
                {
                    AnswersDetail MyUserControl = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as AnswersDetail;
                    MyUserControl.OnUpdate += rgridAnswers_Update;
                    MyUserControl.OnInsert += rgridAnswers_Insert;
                }
            }
     
     
            protected void rgridAnswers_OnRowDrop(object sender, GridDragDropEventArgs e)
            {
                if (e.DraggedItems[0] != null)
                {
                    var orderDrag = GetOrderFromGrid(e.DraggedItems[0]);
                    var orderDest = GetOrderFromGrid(e.DestDataItem);
                    DataManager.Instance.ReOrderAnswers(orderDrag,orderDest);
                    rgridAnswers.Rebind();
                }
            }
     
            private int GetOrderFromGrid(GridDataItem item)
            {
                var orderDragStr = (item.FindControl("hfSortOrder") as HiddenField).Value;
                return int.Parse(orderDragStr);
            }
        }
    }

  2. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 08 Mar 2012 Link to this post

    Hi Jan,

    I reviewed your code and it seems you are not recreating the last loaded control on the subsequent postbacks. Note that in this case, you do not need to rebind them, only add them to the controls collection again.

    Refer to this article for more information:
    http://www.telerik.com/help/aspnet-ajax/ajax-load-user-controls.html

    Kind regards,
    Iana Tsolova
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top