Parent Grid Bind after Update

12 posts, 0 answers
  1. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 11 Jul 2008 Link to this post

    I use a RadWindow to update a row in my grid.  In the RadWindow I click save and have an event to handle the transaction.  However, the window closes but the parent page with the grid does not rebind/refresh with the new data. 

    How do you get it to refresh on a save from a RadWindow?

    All I have so far is to close the window.
    string closingScript;  
    closingScript = "<script type='text/javascript'>window.onload = closeWin</script> ";  
    ClientScript.RegisterStartupScript(this.GetType(), "closeWin", closingScript); 
  2. Shaun Peet
    Shaun Peet avatar
    571 posts
    Member since:
    Aug 2004

    Posted 13 Jul 2008 Link to this post

    The easiest way is to execute a script on the grid's page from within the window's page.  This is the way that I usually do it:

    On the window's page have:

    function get_RadWindow() {   
      var oWindow = null;   
      if (window.radWindow) { oWindow = window.radWindow; }   
      else if (window.frameElement.radWindow) { oWindow = window.frameElement.radWindow; }   
      return oWindow;   
    }  
     
    function CloseAndRebind() {   
      get_RadWindow().BrowserWindow.postBackHF();   
      get_RadWindow().Close();   
    }     
     

    The CloseAndRebind function calls get_RadWindow to get the RadWindow's client object and calls it's BrowserWindow object to get the calling (parent) page object, and then calls a function (postBackHF) on that page.

    On the grid's page place an <asp:hiddenfield /> somewhere on the page (inside an updatepanel if you want to refresh with ajax), and have this:

    var HFID = "<%= HiddenField1.ClientID %>";  
    function postBackHF() { var hF = $get(HFID); if (hF) { hF.value = (new Date()).getTime(); __doPostBack(HFID, ''); } }  
     

    This will force the hidden field to postback with a new value, so in your server-side code setup your code to handle the hidden field's ValueChanged event so that it refreshes the grid.

    I've got a couple other enhancements if you want, but that should get you started.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 20 Jul 2008 Link to this post

    Thanks, but I tought Telerik would have a built in solution.  What other enhancements do you have?

    Thank You!
  5. Shaun Peet
    Shaun Peet avatar
    571 posts
    Member since:
    Aug 2004

    Posted 20 Jul 2008 Link to this post

    Well, you may already be doing this, but on the window's page I would suggest adding a RadAjaxManager, and then add whatever Button (or LinkButton, or FormView) you use to update the form to the collection of ajaxified controls.  To execute the client script after the button's postback, use

    RadAjaxManager1.ResponseScripts.Add("CloseAndRebind()")

    This will eliminate any screen flicker in the window's page and also tends to make it work just a little faster.

    On the parent page, simply put the RadGrid and the HiddenField inside a good 'ole asp:UpdatePanel and the refreshing becomes ajaxified.

    This may be a bit of a philosophical thing, but if you're not using the RadGrid for any of its other features, such as grouping, sorting, filtering, paging, etc. then you'd be better off using either a Repeater or a ListView control as that will speed things up too (sorry, Telerik).

    Now, in response to your comment about Telerik having this built-in.  They do, sort of.  There is a "Pop-Up Edit Form" mode for the Grid (which I think now uses a Dock control instead of a Window) which does automatically rebind the grid after an update.  Demo is here:

    http://www.telerik.com/demos/aspnet/prometheus/Grid/Examples/DataEditing/PopUpEditForm/DefaultCS.aspx

    I have found in my own experience, though, that it's far easier to make the data entry form look good and easy to use by creating my own .aspx page and opening it in a window as you are already doing.  But in terms of what is the easiest possible solution for the developer, what Telerik has built-in is the best.


    Hope that helps,

    Shaun.
  6. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 21 Jul 2008 Link to this post

    I am not a fan of the Telerik way either.  I like to have more control with my forms.

    It is working with the cancel button but not the save.

    RadWindow page
       <script type="text/javascript">    
            function get_RadWindow()   
            {      
              var oWindow = null;      
              if (window.radWindow)   
              {   
              oWindow = window.radWindow;   
              }      
              else if (window.frameElement.radWindow)   
              {  
               oWindow = window.frameElement.radWindow;   
               }      
              return oWindow;      
            }                 
            function CloseAndRebind()   
            {      
              get_RadWindow().BrowserWindow.postBackHF();      
              get_RadWindow().Close();      
            }       
        </script> 


    <asp:Button ID="ButtonSave" Text="Save" OnClick="ButtonSave_Click" CausesValidation="true" Width="100px" runat="server" ValidationGroup="validationSummaryComments" /> 
    <asp:Button ID="ButtonCloseHide" Text="Cancel" Width="50px" CausesValidation="false" OnClientClick="CloseAndRebind();" runat="server" />    

    Code Behind on RadWindow Save.
      protected void ButtonSave_Click(Object obj, EventArgs e)  
        {  
            if (Page.IsValid)  
            {  
              // Do some work  
            }  
            string closingScript;  
            closingScript = "<script type='text/javascript'>window.onload = CloseAndRebind</script> ";  
            ClientScript.RegisterStartupScript(this.GetType(), "CloseAndRebind", closingScript);  
        } 
  7. Shaun Peet
    Shaun Peet avatar
    571 posts
    Member since:
    Aug 2004

    Posted 21 Jul 2008 Link to this post

    I don't have anything small ready to go, but if you can give me a couple of days I can get something up here for you.
  8. Shaun Peet
    Shaun Peet avatar
    571 posts
    Member since:
    Aug 2004

    Posted 22 Jul 2008 Link to this post

    I don't think you need all this:

            string closingScript;  
            closingScript = "<script type='text/javascript'>window.onload = CloseAndRebind</script> ";  
            ClientScript.RegisterStartupScript(this.GetType(), "CloseAndRebind", closingScript);  


    Instead, just use this:

    ClientScript.RegisterStartupScript(this.GetType(),"injectedClose","CloseAndRebind()",true);

    I use VB and not C# so that may not be 100% correct, but that's the gist of it.

    Shaun
  9. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 23 Jul 2008 Link to this post

    I just ended up using their example which I could not find before.

    http://www.telerik.com/demos/aspnet/prometheus/Grid/Examples/DataEditing/PopUpEditForm/DefaultCS.aspx

       ClientScript.RegisterStartupScript(Page.GetType(), "ID", "CloseAndRebind();", true);   
  10. Matthew Botting
    Matthew Botting avatar
    41 posts
    Member since:
    Apr 2005

    Posted 13 Aug 2008 Link to this post

    Hi guys,
    I implemented a similar approach for the radWindow for ASP.NET verion of the control. Sometimes however I would get a javascript error stating that it "can't execute code from a freed script".
    I got around this by wrapping the GetRadWindow().Close(); in a setTimeout wrapper with a 200ms delay
    ie setTimeout('GetRadWindow().Close();',200);

    Since upgrading from the ASP.NET windowmanager to the Q2 ASP.NET AJAX version I am now getting the error about the freed script again. I have tried increasing the timeout as well as using your approach as below

    //clientside function
        function CloseOnReload()
        {  
            GetRadWindow().BrowserWindow.refreshGrid();
            GetRadWindow().Close();
        }

    'Server code
     ClientScript.RegisterStartupScript(Page.GetType(), "TEST", "CloseOnReload();", True)

    Has anyone else experienced this? Also I have been having issues with the close event not firing, so have had to disable debugging in my web.config to get around this.

    Thanks
    Matt
  11. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 13 Aug 2008 Link to this post

    Hello Matthew,

    "can't execute code from a freed script" error can occur if you try to execute code from an already closed and destroyed RadWindow (e.g. you have set sime timeout for the function to be executed, but the page from which it has been called is already destroyed).
    Unfortunately without having a better view over your exact scenario, I cannot provide you with a suitable solution that would work in all cases, but if you open a support ticket and send me a small sample project that reproduces the problem, I will do my best to help.



    Greetings,
    Georgi Tunev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  12. Atiq Ur Rehman
    Atiq Ur Rehman avatar
    66 posts
    Member since:
    Jul 2004

    Posted 14 Aug 2008 Link to this post

    Hi shaun,

    I am trying to do this but nothing happens in my case. Can you please help me out with the code? I am doing exactly the same thing but my child window closes it self thats it, no ajax post back nothing on the parent page.
    Please help.

    thanks
    Atiq
  13. Shaun Peet
    Shaun Peet avatar
    571 posts
    Member since:
    Aug 2004

    Posted 14 Aug 2008 Link to this post

    Hello Atiq,

    I would be glad to try and help you.  Please post the code that you are using to open and to close the window.  Thanks,

    Shaun.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017