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

.RegisterPostBackControl fails after RadGrid AsyncPostBack

7 Answers 516 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 03 Mar 2010, 05:56 PM
Hi there,

I have searched high and low for an answer to this, however can't seem to get any of them working, so here goes...

I have a WebForm with a single RadGrid Q3 2009 and a RadAjaxManager control.  The RadGrid has AllowFilteringByColumn enabled. The WebForm has a MasterPage, which has RadScriptManager and a RadAjaxLoadingPanel. In the RadGrid, one of my columns is an ImageButton GridButtonColumn (with RadWindow Confirm).  In the RadGrid1_ItemDataBound I set the CommandArgument of the button.  In RadGrid1_ItemCreated, I use Master.FindControl to find the RadScriptManager, and call .RegisterPostBackControl(button).  This causes the ImageButton to create a full PostBack as expected.

The problem is that this stops working after an AsyncPostBack has occurred.  For instance, when the RadGrid is filtered on any column, an AsyncPostBack occurs.  If I click on the same button which caused a full PostBack before, it now causes another AsyncPostBack which I do not want. I'd really appreciate any help, I have put together a simple WebSite project to demonstrate this, which I will email if you need it.

Essentially, the important code is as follows:
Master Page:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
    <asp:ContentPlaceHolder id="head" runat="server">  
    </asp:ContentPlaceHolder> 
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
         <telerik:RadAjaxLoadingPanel  ID="RadAjaxLoadingPanel1" runat="server" > 
        <img alt="Loading..." src='<%= RadAjaxLoadingPanel.GetWebResourceUrl(Page, "Telerik.Web.UI.Skins.Default.Ajax.loading.gif") %>' style="border:0;" /> 
        </telerik:RadAjaxLoadingPanel>    
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">  
          
        </asp:ContentPlaceHolder> 
    </div> 
    </form> 
</body> 
</html> 

WebForm:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
</asp:Content> 
 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">  
        <AjaxSettings> 
            <telerik:AjaxSetting AjaxControlID="RadGrid1">  
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" /> 
                    <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
        </AjaxSettings> 
    </telerik:RadAjaxManager>    
          
    <telerik:RadGrid ID="RadGrid1" runat="server"   
        AllowPaging="True"    
        VirtualItemCount="100" 
        datasourceid="ObjectDataSource1" 
        GridLines="None"   
        Skin="Windows7"   
        AllowFilteringByColumn="True"    
        EnableLinqExpressions="false" 
        onitemcommand="RadGrid1_ItemCommand"   
        onitemcreated="RadGrid1_ItemCreated"   
        onitemdatabound="RadGrid1_ItemDataBound">    
        <GroupingSettings CaseSensitive="false" />           
        <MasterTableView autogeneratecolumns="False" DataKeyNames="ID" > 
            <PagerStyle AlwaysVisible="true" /> 
            <Columns> 
                <telerik:GridButtonColumn ConfirmText="View this item?" ConfirmDialogType="RadWindow" 
                    ConfirmTitle="View Item" ButtonType="ImageButton" CommandName="View" Text="View" 
                    UniqueName="ViewColumn" ItemStyle-Width="15px" ImageUrl="~/Images/newContact.gif">  
                </telerik:GridButtonColumn>                              
                  
                <telerik:GridBoundColumn DataField="Name" HeaderText="Name"   
                    SortExpression="Name" UniqueName="Name" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true">  
                </telerik:GridBoundColumn>                  
            </Columns> 
        </MasterTableView> 
        <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle> 
         
    </telerik:RadGrid> 
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"   
        EnablePaging="true" 
        DataObjectTypeName="Title"   
        OldValuesParameterFormatString="original_{0}"   
        SelectMethod="GetTitles"   
        SelectCountMethod="GetTitlesCount" 
        TypeName="DataAccess">  
    </asp:ObjectDataSource> 
    <telerik:RadWindowManager  ID="RadWindowManager1" runat="server"></telerik:RadWindowManager> 
      
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
 </asp:Content> 
 
 

Code-Behind:
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using Telerik.Web.UI;  
 
public partial class _Default : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
 
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
        string commandname = e.CommandName;  
        string commandArgument = e.CommandArgument.ToString();  
 
        if (commandname.Equals("View") && !string.IsNullOrEmpty(commandArgument))  
        {  
            int id = int.Parse(commandArgument);  
 
            Label1.Text = "You clicked item id: " + id.ToString();  
        }  
    }  
 
    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)  
    {  
        // make the View ImageButtons PostBackControls  
        if (e.Item is GridDataItem)  
        {  
            GridDataItem item = (GridDataItem)e.Item;  
            ImageButton viewButton = (ImageButton)item["ViewColumn"].Controls[0];  
 
            ScriptManager.GetCurrent(Page).RegisterPostBackControl(viewButton);  
 
            //viewButton.Attributes.Add("onclick", string.Format("realPostBack(\"{0}\", \"\"); return false;", viewButton.UniqueID));  
            //viewButton.OnClientClick = String.Format("{0}.__doPostBack(\"{1}\",\"{2}\");return false;", RadGrid1.ClientID, viewButton.UniqueID, viewButton.CommandArgument);  
            //RadScriptManager rsm = Master.FindControl("RadScriptManager1") as RadScriptManager;  
            //if (rsm != null)  
            //{  
            //    rsm.RegisterPostBackControl(viewButton);  
            //}  
        }  
    }  
 
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            GridDataItem item = (GridDataItem)e.Item;  
            (item["ViewColumn"].Controls[0] as ImageButton).CommandArgument = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString(); ;  
        }  
    }  


I have tried adding the __doPostBack(...) Javascript call on to the ImageButton instance per the solution in this post http://www.telerik.com/help/aspnet-ajax/ajxexclude.html, but that didn't work for me. I have tried using a regular ScriptManager instead of the RadScriptManager too, to no avail.

Many thanks,

Anthony

7 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 08 Mar 2010, 02:47 PM
Hi Anthony,

Could you please verify that you are using the latest version of the RadControls for ASP.NET AJAX.

Additionally you can disable ajax for a specific button directly from the AjaxManager by hooking up to the client-side OnRequestStart event:

<script type="text/javascript"
  function mngRequestStarted(ajaxManager, eventArgs) 
  
      if(eventArgs.EventTarget == "MyButton"
      
           eventArgs.set_enableAjax(false);
      
  
</script>

Also I am sending you a simple example which illustrates the desired functionality.

All the best,
Radoslav
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
alexandrupaul
Top achievements
Rank 1
answered on 25 Mar 2010, 02:33 PM
Hi

I have the same issue.
i have an imagebutton inside a radgrid template column. radgrid is ajaxfied using the ajaxmanager on the masterpage.
i want the imagebutton to do a full postback and i have tried using imagebutton_prerender, radgrid_itemcreated and radgrid_itemdatabound and i put the code: ScriptManager.GetCurrent(Page).RegisterPostBackControl(b);

It  only works first time, if an async operation is made on the radgrid like sorting, filtering or paging the imagebutton won't make a full postback.


0
Anthony
Top achievements
Rank 1
answered on 29 Mar 2010, 01:03 PM
Hi all,

I rewrote/retested my original code, and now have it working as expected without using any of the fixes or workarounds! :-)

Anthony

Hugo Augusto
Top achievements
Rank 2
Iron
Veteran
Iron
commented on 02 Dec 2022, 09:25 PM | edited

What did you do to avoid using the RadAjaxManager.ClientEvents-OnRequestStart event workaround?

I found that setting PostBackControls of the RadAjaxManager control solves all the issues. In my case, my RadAjaxManager is on a MasterPage and need to get to it from a UserControl.

RadAjaxManager MyAjaxManager = (RadAjaxManager)this.Page.Master.FindControl("MyAjaxManager");
                MyAjaxManager.PostBackControls = new string[] { btnExport.ID };
0
Radoslav
Telerik team
answered on 30 Mar 2010, 10:49 AM
Hi Alexandru,

I could not reproduce the described issue. Could you please elaborate a bit more about your scenario. Also it will be helpful if you could send us a simple runnable application demonstrating the problem. You could open a formal support ticket from your Telerik account and attach a ZIP file there.  In that way we can reproduce and pinpoint the problems you're facing on our side, understand the logic of your application and provide a solution.

Best wishes,
Radoslav
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
Rosemary
Top achievements
Rank 1
answered on 13 Apr 2010, 05:33 PM
I have the same issue as 'alexandrupaul '
Any feedback on a possible solution?

-----
I have  a radgrid on my page and it's 'ajaxified' via a RadAjaxManagerProxy control on the same page.

I have tried the following code in order to make my export buttons work. They work the first time around. But after an asnyc postback, the export buttons do not seem to do anything.

 

 protected void rdgrid_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridCommandItem)  
            {  
                e.Item.FindControl("InitInsertButton").Visible = false;  
                e.Item.FindControl("AddNewRecordButton").Visible = false;  
 
                RadScriptManager.GetCurrent(Page).RegisterPostBackControl(e.Item.FindControl("ExportToWordButton"));  
                RadScriptManager.GetCurrent(Page).RegisterPostBackControl(e.Item.FindControl("ExportToExcelButton"));  
                RadScriptManager.GetCurrent(Page).RegisterPostBackControl(e.Item.FindControl("ExportToPdfButton"));  
                RadScriptManager.GetCurrent(Page).RegisterPostBackControl(e.Item.FindControl("ExportToCsvButton"));  
            }  
        } 

 

 

0
Radoslav
Telerik team
answered on 16 Apr 2010, 08:56 AM
Hello Rosemary,

You could try handling the RadAjaxManager.ClientEvents-OnRequestStart event and if the export button is clicked to disable the ajax:
<telerik:RadAjaxManager ClientEvents-OnRequestStart="mngRequestStarted" ID="RadAjaxManager1" runat="server">
  <AjaxSettings>
   <telerik:AjaxSetting AjaxControlID="RadGrid1">
     <UpdatedControls>
     <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
     <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" />
     </UpdatedControls>
   </telerik:AjaxSetting>
  </AjaxSettings>
</telerik:RadAjaxManager>

<telerik:RadCodeBlock runat="server">
  <script type="text/javascript">
      var disableAjax = false;
      function mngRequestStarted(ajaxManager, eventArgs) {
        if (disableAjax) {
          disableAjax = false;
          eventArgs.set_enableAjax(false);
        }
      }
      function ExportButtonClick() {
         disableAjax = true;
      }
 
   </script>
</telerik:RadCodeBlock>

On RadGrid.ItemCreated event handler you could find the export buttons and add ExportButtonClick() function as a handler to the onclick event:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
   if (e.Item is GridCommandItem)
   {
       e.Item.FindControl("InitInsertButton").Visible = false;
       e.Item.FindControl("AddNewRecordButton").Visible = false;
 
       Button buttonExportToWord = e.Item.FindControl("ExportToWordButton") as Button;
       buttonExportToWord.Attributes.Add("onclick", " ExportButtonClick ();");
       Button buttonExportToExcel = e.Item.FindControl("ExportToExcelButton") as Button;
       buttonExportToExcel.Attributes.Add("onclick", " ExportButtonClick ();");
       Button buttonExportToPdf = e.Item.FindControl("ExportToPdfButton") as Button;
       buttonExportToPdf.Attributes.Add("onclick", " ExportButtonClick ();");
       Button buttonExportToCsv = e.Item.FindControl("ExportToCsvButton") as Button;
       buttonExportToCsv.Attributes.Add("onclick", " ExportButtonClick ();");
        
    }

Additionally I am sending you a simple example which demonstrates the desired functionality.

All the best,
Radoslav
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
Rosemary
Top achievements
Rank 1
answered on 21 Apr 2010, 08:37 PM
Thanks Radoslav,

I actually found a similar solution provided in the help section: http://www.telerik.com/help/aspnet-ajax/grdexportwithajax.html
The javascript option worked for me. 




Tags
Grid
Asked by
Anthony
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
alexandrupaul
Top achievements
Rank 1
Anthony
Top achievements
Rank 1
Rosemary
Top achievements
Rank 1
Share this question
or