Friday, July 3, 2009

Using JQuery UI Dialog with ASP.Net and AJAX Update Panel

One of the first things you will notice when you try and use JQuery UI Dialog in ASP.Net page is it does not perform post backs from within the dialog window. This is because in a ASP.Net page there is only one form element and Jquery appends the dialog "div" outside of the form element. There is a easy way to fix this. And once its done then its simple matter of combining some ASP.Net AJAX Update Panels and you are ready to go.

Lets look at a simple but non-trivial example of combining JQuery UI Dialog, Asp.Net and AJAX Update Panel. You can download the source of the sample here. Our sample application will display a list of names. To edit a name, you click on a name. 

This will pop up a JQuery UI Dialog with the current name. You can edit the name and click save. This will save and automatically close the dialog.   For adding a new Name, click on the "Add New Person" button. And this will bring up the new person dialog and allow you to save. Adding or Editing names will automatically update the list with the changes.









I am not going to go through explaining each line of the source. Its much easier to read the source code for yourself. I will just point out the main highlights. The first thing you will notices is when setting up the JQuery dialog, we are adding a open event to attach the dialog div to the form element. This will insure the post backs to happen.


 

 

 



 

 

 

Secondly, We set up update panels for each of the dialogues. It is important to note that the Update Panels are nested inside the dialog div. And also the GridView to display the list is also within an Update Panel. All of our update panels, UpdateMode is set as "Conditional" and ChildrenAsTriggers as "true". So they only update when some control within them posts back and we can also trigger the update by their Update method from the server side.

OK, now the code behind. To keep thing simple this example uses a Dictionary to store the names in the viewstate. Most of the code here is self explanatory. However I will point out the use of "ScriptManager.RegisterClientScriptBlock" to close the dialog. RegisterClientScriptBlock helps in adding javascript to be executed on asynchronous post back.

That's pretty much it. Give me a yell if you have any questions or some suggestions.

UPDATE: Also check out Using jQuery UI Tabs with ASP.Net MVC and Ajax Form Submit

Happy Coding

Ravi

Download Sample Code

47 comments:

  1. AnonymousJul 15, 2009 05:49 PM
    nice!
    ReplyDelete
  2. SpaceGhostJul 21, 2009 12:01 PM
    Awesome. I have been struggling with this for a week. TYVM.
    ReplyDelete
  3. SpaceGhostJul 23, 2009 08:37 AM
    Made some small modifications to how your code works to accommodate server side lagging behind the client side because of db processing or other factors. If you want I can email you the changes.
    ReplyDelete
  4. BigJasonAug 20, 2009 09:05 PM
    I am using this, and it is working better. However calling the dialog() function changes the main form to no longer point to the appropriate action and instead is pointing action to "javascript:void(0);". Any one else run into this?
    ReplyDelete
  5. BryanSep 25, 2009 05:05 PM
    Thanks! The odd behavior of jQuery and the UpdatePanel was driving me crazy.
    ReplyDelete
  6. hhOct 7, 2009 03:53 AM
    Thanks for the post. Its really help me a lot. I want another help that how can i make background as read only or inactive when opening a dialog using JQuery like i did using ajaxtoolkitpopdialog.

    Thanks,
    Ashok Mohanty
    ReplyDelete
  7. Rich AlgerOct 7, 2009 01:23 PM
    Thank so much for this. I made a note of it for my reference on my blog.
    ReplyDelete
  8. AnonymousDec 5, 2009 05:01 PM
    Beautiful, thanks!
    ReplyDelete
  9. AnonymousDec 11, 2009 03:08 PM
    I was getting a similar problem with the jQuery SimpleModal, second time you clicked on a link to the modal it would be blank. I was using an EndRequestHandler to try and do fix it but had to close then open the modal to recreate it every time but your approache fixed it everything. And it's even easier using the Simple modal as it has an appendTo: 'form' parameter which you can use instead the appendTo form command on the open function.

    Thanks Ravi!
    ReplyDelete
  10. RichardDec 21, 2009 03:52 AM
    A simple approach is to put the dialog-HTML back into the updatepanel, not just the form.

    If you append the dialog-HTML to the form you will end up having duplicates when the updatepanel is refreshed again.

    I always do this:
    function showDialog(selector) {
    var source = $(selector).parent();
    $(selector).dialog({open: function(type, data) { $(this).parent().appendTo(source); }});
    }

    And the register a startup-script:
    showDialog('#myDialogContainer');

    No need for either serverside nor clientside duplicate-handling.
    ReplyDelete
  11. RichardDec 21, 2009 04:53 AM
    Regarding my post above:
    That is, if you are posting the HTML for the dialog in the UpdatePanel.
    In the example in the article the HTML for the dialogs are static.
    ReplyDelete
  12. AnonymousJan 27, 2010 01:08 PM
    Thanks So much saved my life..

    i'm using anthem.net and im calling Anthem_InvokePageMethod to execute a method on the server. the server became out of sync with the HTML due to the JQUERY moving the DIV.
    thanks again....
    ReplyDelete
  13. AnonymousMar 5, 2010 09:58 PM
    Thank you for your post, it was very helpful.
    ReplyDelete
  14. AnonymousMar 16, 2010 07:39 AM
    very nice! Well done!
    ReplyDelete
  15. AnonymousMar 31, 2010 01:29 PM
    Great! It saved my day!
    ReplyDelete
  16. RafaelApr 12, 2010 11:01 AM
    Really Helpful!! Congratulations!! =)
    ReplyDelete
  17. PabloApr 26, 2010 05:53 PM
    It was really helpful! thanks!! just a comment, if you modify "showDialog" function in the aspx file, you can use only one dialog.
    function showDialog(id, title) {
    $('#' + id).dialog( "option" , "title" , title );
    $('#' + id).dialog("open");
    }
    ReplyDelete
  18. nova5May 10, 2010 07:23 AM
    Thanks, was just about to bin using jQuery UI with ASP.NET.. :)
    ReplyDelete
  19. AnonymousMay 24, 2010 05:50 PM
    Thanks for this article. It saved me as well. I was struggling with this for the better part of a day. You were absolutely right. It never crossed my mind to look to see if the modal dialog div was outside the form. I was beating my head against the wall trying to figure out why my controls were losing their state upon postback. You are the man!
    ReplyDelete
  20. RinaJul 5, 2010 06:48 AM
    it's great but i need something like loading...
    till the data not come in modal

    plz help
    ReplyDelete
  21. Ruben RiveraAug 4, 2010 10:22 AM
    Love you men!
    ReplyDelete
  22. hieu leAug 9, 2010 02:39 AM
    thanks so much, it work very well
    ReplyDelete
  23. vodkaAug 19, 2010 07:58 PM
    i love it. Thanks YOU very muck
    ReplyDelete
  24. AnonymousAug 19, 2010 09:58 PM
    Great post. Thanks
    ReplyDelete
  25. milleSep 21, 2010 10:18 AM
    I love you :D Thanks alot!

    Btw, it would be enough to move the dialog just once instead of each time the dialog opens:

    $('#dialog').dialog({
    autoOpen: false,
    [...]
    });
    $('#dialog').parent().appendTo('form');
    ReplyDelete
  26. AnonymousSep 22, 2010 04:00 PM
    I too have been struggling with this issue, but unfortunately am still struggling. I've used the method this blog item promotes and the one Mille suggests too. In both instances, I'm rewarded by the error message:

    Node cannot be inserted at the specified point in the hierarchy" code: "3

    What/where should I be looking for the error in my code?

    Thanks/Bruce
    ReplyDelete
  27. WahaB KotwalNov 13, 2010 11:32 PM
    thanks a million man..!! you saved my life! :)
    ReplyDelete
  28. EvolvingNov 20, 2010 01:44 PM
    Thanks you are a life saver!!
    ReplyDelete
  29. agameNov 26, 2010 07:48 AM
    nice post and helpful comments, bookmarking this one. i have several projects I could use this on - jquery modals rock

    thanks!
    ReplyDelete
  30. BobDec 18, 2010 12:37 PM
    Just what I needed. Thank you!
    ReplyDelete
  31. AnonymousJan 12, 2011 07:37 AM
    too much appreciated, exactly what i was loooking for!
    ReplyDelete
  32. AnonymousFeb 17, 2011 07:31 PM
    I just wasted hours. Thanks for this post! It was driving me crazy!
    ReplyDelete
  33. AnonymousMar 24, 2011 07:45 AM
    Thks a lot
    ReplyDelete
  34. ManojMay 2, 2011 11:39 AM
    Thanks a lot ....
    Good Example.....
    Appreciated that already added JQuery in ASPX and CS File.
    Excellent,
    ReplyDelete
  35. AnonymousMay 17, 2011 12:59 AM
    Thank so much
    ReplyDelete
  36. WahaB KotwalMay 30, 2011 11:33 AM
    thanks a lot.. :)
    ReplyDelete
  37. AnonymousJul 5, 2011 06:38 AM
    Very good ! Works :)
    ReplyDelete
  38. my-entropyAug 22, 2011 10:09 PM
    Thanks Ravi, this is working fine, but I notice one issue while editing the data , sometimes, the data loading to the control is getting delay. Did u notice that, sometimes the postback getting delay, if u have solution for that issue pls send to me the solution to mail.ablealias@gmail.com. Thanks
    ReplyDelete
  39. AnonymousSep 19, 2011 03:31 AM
    Thanks a lot :) This finally saved my day.
    ReplyDelete
  40. AnonymousOct 24, 2011 11:24 AM
    Great! it's a good job.
    ReplyDelete
  41. AnonymousOct 26, 2011 10:03 AM
    Thanks for the tip!
    ReplyDelete
  42. AnonymousOct 27, 2011 05:30 AM
    Thanks so much for this tip
    ReplyDelete
  43. SathiyaNov 3, 2011 08:02 AM
    Thanks Ravi...Wonderful job!!!
    ReplyDelete
  44. AnonymousNov 8, 2011 05:19 AM
    Estimado:
    Tengo una duda, usted utiliza el dialog de Jquery con botones asp.net, pero que pasa cuando utilizamos objeto con sus elementos originales como por ejemplo:
    $("#dialog-Usuario").dialog({
    open: function () { { $(this).parent().appendTo($("form:first")); } },
    resizable: false,
    height: 120,
    width: 230,
    modal: true,
    show: "scale",
    hide: "scale",
    buttons: {
    "Aceptar": function () {
    $(this).dialog("close");
    __doPostBack('UpdatePanel2', 'ValidarUsuario');
    },
    "Cancelar": function () {
    $(this).dialog("close");
    }
    }
    });
    Resulta que __doPostBack queda con el valor 'UpdatePanel2' y no limpia Request["__EVENTTARGET"] del código trasero a pesar de presionar otro botón de asp.net.
    ReplyDelete
  45. PaulyDec 2, 2011 08:44 AM
    Using the dialog in a Page works great but I couldn't get the closeDialog to fire in a webcontrol... you need to register the script like this: ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), UniqueID, script, true);

    My webcontrol works great now. Thanks a lot for a terrific article.
    ReplyDelete
  46. Bruno OliveiraJan 6, 2012 03:17 AM
    Very Useful, could you authorize me translate this post to portuguese and put in my blog?
    I will give credit to you as autor

    Bruno Gomes
    ReplyDelete
  47. RaviJan 6, 2012 01:00 PM
    Sure. go ahead.
    ReplyDelete