Rad controls breaks down after manual ajax request with RaisePostBackEvent()

2 posts, 0 answers
  1. Peter Bogoyavlensky
    Peter Bogoyavlensky avatar
    20 posts
    Member since:
    Feb 2009

    Posted 11 Jun 2012 Link to this post

    What I'm doing:
    1. Put two RadAjaxPanels on a page.
    2. Put one Rad controls in one of these panels and a simple asp.net control in the other one.
    3. Changes made in one of these panels should refresh another panel but don't touch the panel where is the change was made.
    4. Do any action in the panel with RadControl - panel with asp.net control will refresh correctly.
    5. Do any change with asp.net control - panel with Rad control will breaks down and doesn't fire any events anymore.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxProject_1.Default" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title>Default page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <tel:RadScriptManager runat="server" ID="Rsm1"></tel:RadScriptManager>
        <tel:RadAjaxManager runat="server" ID="Ram1"></tel:RadAjaxManager>
        <tel:RadScriptBlock runat="server">
            <script language="javascript">
                function __doXBack(args) {
                    var ram = $find("<%= Ram1.ClientID %>");
                    if (ram) ram.ajaxRequest(args);
                }
     
                function Refresh1(sender, args) {
                    var value = args.getDataKeyValue("Id");
                    if (value) __doXBack("1|" + value);
                }
     
                function Refresh2(ddl) {
                    __doXBack("2|" + ddl.options[ddl.selectedIndex].value);
                }
            </script>
        </tel:RadScriptBlock>
     
        <tel:RadAjaxPanel runat="server" ID="Rap1">
            <asp:DropDownList runat="server" ID="Rcb1" onChange="Refresh2(this)"></asp:DropDownList>
        </tel:RadAjaxPanel>
     
        <hr/>
     
        <tel:RadAjaxPanel runat="server" ID="Rap2">
            <tel:RadGrid runat="server" ID="Grid1">
                <ClientSettings>
                    <Selecting AllowRowSelect="True"></Selecting>
                    <ClientEvents OnRowSelected="Refresh1"></ClientEvents>
                </ClientSettings>
                <MasterTableView ClientDataKeyNames="Id"></MasterTableView>
            </tel:RadGrid>
        </tel:RadAjaxPanel>
        </form>
    </body>
    </html>


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using Telerik.Web.UI;
     
    namespace AjaxProject_1
    {
        public partial class Default : System.Web.UI.Page
        {
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                 Ram1.AjaxRequest += Ram1OnAjaxRequest;
            }
     
            private void Ram1OnAjaxRequest(object sender, AjaxRequestEventArgs e)
            {
                if (e == null || string.IsNullOrEmpty(e.Argument)) return;
                 if (e.Argument.StartsWith("1|"))
                {
                    InitComboBox(e.Argument);
                    Rap1.RaisePostBackEvent(null);
                }
                if (e.Argument.StartsWith("2|"))
                {
                    InitGrid();
                    Rap2.RaisePostBackEvent(null);
                }
            }
     
            protected void Page_Load(object sender, EventArgs e)
            {
                if (IsPostBack) return;
                 InitComboBox("0");
                InitGrid();
            }
     
            private void InitComboBox(string id)
            {
                Rcb1.Items.Clear();
                 var list = new List<string> {id};
                for (var i = 0; i < 10; i++) list.Add(GetRandomPropertyValue());
                Rcb1.DataSource = list;
                Rcb1.DataBind();
            }
     
            private static readonly IList<string> Properties = new[] {"Id", "Name"};
            private void InitGrid()
            {
                var data = new DataTable();
                Array.ForEach(Properties.ToArray(), t => data.Columns.Add(new DataColumn(t, typeof(string))));
                for (var i = 0; i < 10; i++)
                {
                    var row = data.NewRow();
                    row["Id"] = i.ToString();
                    row["Name"] = GetRandomPropertyValue();
                    data.Rows.Add(row);
                }
                 Grid1.DataSource = data;
                Grid1.DataBind();
            }
     
            private static IList<char> _chars;
            private static readonly Random Rnd = new Random(DateTime.Now.Millisecond);
            private static string GetRandomPropertyValue()
            {
                if (_chars == null)
                {
                    _chars = new List<char> {' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
                    for (var c = 'a'; c <= 'z'; c++) _chars.Add(c);
                    for (var c = 'A'; c <= 'Z'; c++) _chars.Add(c);
                }
     
                var len = Rnd.Next(10, 15);
                var sb = new StringBuilder();
                for (var i = 0; i < len; i++) sb.Append(_chars[Rnd.Next(0, _chars.Count - 1)]);
                return sb.ToString();
            }
        }
    }


    I know that I can get that behaviour from page with another methods like AjaxUpdatedControls in AjaxManager and so on, but I need to update panels like I described. It is some kind of specific application and I have to use this way to update controls on page.
    Unfortunately it is not working.
    Any ideas how to make it working?
  2. Vasil
    Admin
    Vasil avatar
    1641 posts

    Posted 13 Jun 2012 Link to this post

    Hi Peter,

    You have two correct approaches on this one:

    First would be to use RadAjaxManager with 2 asp:Panels and set correct settings, but as you said earlier, this is not what you desire.

    Another approach is to remove the RadAjaxManager completely from your page, because currently it does the Ajax, so it includes scripts for initializing client controls only for its settings, however since there are no settings, no controls are initialized on client side. The Dropdown works because it does not have any scripts on the page. You could do the same logic as your current one without manager, and the controls will be updated correctly. here is the code:
    Aspx:
    <script type="text/javascript">
     
        function Refresh1(sender, args)
        {
            var value = args.getDataKeyValue("Id");
            var rap1 = $find("<%= Rap1.ClientID %>");
            rap1.ajaxRequest("1|" + value);
        }
     
        function Refresh2(ddl)
        {
            var rap2 = $find("<%= Rap2.ClientID %>");
            rap2.ajaxRequest("2|" + ddl.options[ddl.selectedIndex].value);
        }
    </script>

    C#
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Rap1.AjaxRequest += Ram1OnAjaxRequest;
        Rap2.AjaxRequest += Ram1OnAjaxRequest;
     
    }
     
    private void Ram1OnAjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        if (e == null || string.IsNullOrEmpty(e.Argument)) return;
        if (e.Argument.StartsWith("1|"))
        {
            InitComboBox(e.Argument);
            Rap1.RaisePostBackEvent(null);
        }
        if (e.Argument.StartsWith("2|"))
        {
            InitGrid();
            Rap2.RaisePostBackEvent(null);
        }
    }

    Greetings,
    Vasil
    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