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

RadGrid Client object is null on Ajax postback

1 Answer 22 Views
Grid
This is a migrated thread and some comments may be shown as answers.
SHERFUDEEN
Top achievements
Rank 1
SHERFUDEEN asked on 02 Jan 2015, 02:13 PM
Hi,
I have a radgrid with checkbox(Template columns) column. By default the checkboxes are selected (written in Itemdatabound) and row is in green color. If the user deselects the checkbox then the row should be in default color.
I wrote the following javascript for checkbox click event.

function GridCheckboxChecked(obj1, rowid) {
                    
                    //ensure that the outer grid dataItem are generated
                    var grid = $find("<%= rgReceivers.ClientID %>");
                    grid.get_masterTableView().get_dataItems();

                    var griditem = $find(rowid);
                    griditem.set_selected(obj1.checked);
                    var girdrow = $('#' + rowid)[0];

                    if (obj1.checked) {
                       
                        $(girdrow).removeAttr("style");
                        $(girdrow).attr("style", "background-color:#C9F1C9");
                    }
                    else {
                        $(girdrow).removeAttr("style");
                    }
                }


It works fine. But if any other ajax request is performed in the screen, all the rows are coming back in Green color again.

I found that there is no method available to track the client side changes for radgrid.
So I reapplied the changes in the radgrid when ajax postback. For that I wrote the following in document.ready function

$(document).ready(function () {
                   
                    var grid = $find("<%= rgReceivers.ClientID %>");
                    console.log(grid);
                    
                    if (grid != null) {
                        var dataItems = grid.get_masterTableView().get_dataItems();

                        for (var i = 0; i < dataItems.length; i++) {
                           // re apply the changes
                        }
                    }
                });

When the page loaded first , it founds the grid object. But for the proceeding ajax calls, the grid object is showing as null.

Following are my ajax settings (applied on page load event)
    
      RadAjaxManager mgr = RadAjaxManager.GetCurrent(Page);
        mgr.AjaxSettings.Clear();
        mgr.AjaxRequest += new RadAjaxControl.AjaxRequestDelegate(mgr_AjaxRequest);
        mgr.AjaxSettings.AddAjaxSetting(mgr, pnlContent, (RadAjaxLoadingPanel)(this.Page.Master as MasterPage).FindControl("RadAjaxLoadingPanel2"));
        mgr.AjaxSettings.AddAjaxSetting(btnsendbox, rgReceivers, (RadAjaxLoadingPanel)(this.Page.Master as MasterPage).FindControl("RadAjaxLoadingPanel2"));
        mgr.AjaxSettings.AddAjaxSetting(btnsendbox, pnlContent);
        mgr.AjaxSettings.AddAjaxSetting(rgReceivers, pnlContent);
        mgr.AjaxSettings.AddAjaxSetting(rgReceivers, rgReceivers, (RadAjaxLoadingPanel)(this.Page.Master as MasterPage).FindControl("RadAjaxLoadingPanel2"));
        mgr.AjaxSettings.AddAjaxSetting(btnClear, pnlContent, (RadAjaxLoadingPanel)(this.Page.Master as MasterPage).FindControl("RadAjaxLoadingPanel2"));
        mgr.AjaxSettings.AddAjaxSetting(btnSave, pnlContent, (RadAjaxLoadingPanel)(this.Page.Master as MasterPage).FindControl("RadAjaxLoadingPanel2"));
        mgr.AjaxSettings.AddAjaxSetting(chkCBN, pnlContent, (RadAjaxLoadingPanel)(this.Page.Master as MasterPage).FindControl("RadAjaxLoadingPanel2"));
        mgr.AjaxSettings.AddAjaxSetting(cmbRecalNos, pnlContent, (RadAjaxLoadingPanel)(this.Page.Master as MasterPage).FindControl("RadAjaxLoadingPanel2"));
        mgr.AjaxSettings.AddAjaxSetting(chkAll, pnlContent, (RadAjaxLoadingPanel)(this.Page.Master as MasterPage).FindControl("RadAjaxLoadingPanel2"));


Why the grid object is showing as null for the ajax postbacks even though it is on the page. How to find the radgrid on ajax postbacks?

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 07 Jan 2015, 07:20 AM
Hello Sherfudeen,

If you need to execute some logic on initial load and after every AJAX request, you should handle the pageLoad instead of the $(document).ready():
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script type="text/javascript">
        function pageLoad() {
            // your logic
        }
    </script>
</telerik:RadCodeBlock>

Some detailed information regarding the difference between pageLoad and $(document).ready() is available in the following article:
Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
SHERFUDEEN
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or