views:

530

answers:

3

Hi all,

I've got a Jquery UI dialog that pops up to confirm the creation of an item after filling out a form. I have the form in an update panel due to various needs of the form, and especially because I want validation being done on the form without reloading the page.

JavaScript appears to not reload on an asynchronoous postback. This means when the form is a success and I change the variable 'formSubmitPass' to true, it does not get passed to the Javascript via <%= formSubmitPass %>. If I add a trigger to the submit button to do a full postback, it works. However I don't want the submit button to do a full postback as I said so I can validate the form within the update panel.

How can I have this so my form validates asynchronously, but my javaScript will properly reload when the form is completed successfully and the item is saved to the database?

Javascript:

    var formSubmitPass = '<%= formSubmitPass %>';
var redirectUrl = '<%= redirectUrl %>';

function pageLoad() {

    $('#formPassBox').dialog({
        autoOpen: false,
        width: 400,
        resizable: false,
        modal: true,
        draggable: false,
        buttons: {
            "Ok": function() {
                window.location.href = redirectUrl;
            }
        },
        open: function(event, ui) {
            $(".ui-dialog-titlebar-close").hide();
            var t = window.setTimeout("goToUrl()", 5000);
        }
    });

    if(formSubmitPass == 'True')
    {
        $('#formPassBox').dialog({
            autoOpen: true
        });
    }

So how can I force a postback from the code behind, or reload the JavaScript on an Asynchronous Postback, or do this in a way that will work such that I can continue to do Async form validation?

Edit: I change formSubmitPass at the very end of the code behind:

If errorCount = 0 Then
            formSubmitPass = True
            upForm.Update()
        Else
            formSubmitPass = False
        End If

So on a full postback, the value does change.

A: 

Not exactly sure what you're trying to do. <%= formSubmitPass %> is certainly not going to be re-evaluated because that is a server-variable that is set only upon page reload. Even if you'd execute the javascript again, the value wouldn't change.

However, you can also pass a succes callback to the .dialog() method:

$('#formPassBox').dialog({
  // ...
  success: function(){ 
    // do something, e.g.
    $("#formPassBox").dialog({autoOpen:false});
  }
});
mnemosyn
See my edit: It does change on page reload. As I said I got this working if I don't have an update panel, or if I set the submit button's trigger within the update panel to do a fullpostback. The value does change, and it works.But I don't know how keep my submit button doing a Async postback (for form validation purposes), while getting the code to run again. How would I have the code behind pass that success to the front end?
sah302
+2  A: 

I sounds more like the html is being replaced by the update panel and your jQuery selectors are not being bound to the new elements.

Try using .Live for all your events, or rebinding after an ajax update (you can use UpdatePanel plug-in for this)

James Westgate
Nope pretty sure that isn't it either, I've got other dialogs that are triggered by clicking on links (in the update panel), and when I do an async postbacks, the dialogs are still bound and still work. If the dialogs weren't being bound to the selectors, the div selectors would be visible on the page, but they are still invisible.
sah302
The .live instead of .bind in my situation worked. Thanks.
Colour Blend
A: 

I figured this out, what worked for me is first putting the dialog javascript in the page_load to just have a the auto open property set to false like so:

function pageLoad() {

    $('#formPassBox').dialog({
        autoOpen: false
     });
}

Then create a totally separate function like so:

function formSubmitPass() {
    $('#formPassBox').dialog({
        autoOpen: true,
        width: 400,
        resizable: false,
        modal: true,
        draggable: false,
        buttons: {
            "Ok": function() {
                window.location.href = redirectUrl;
            }
        },
        open: function(event, ui) {
            $(".ui-dialog-titlebar-close").hide();
            var t = window.setTimeout("goToUrl()", 10000);
        }
    });
}

Then use RegisterStartupScript as indicated by these questions: http://stackoverflow.com/questions/2794850/run-javascript-after-form-submission-in-update-panel and http://stackoverflow.com/questions/2294039/what-is-wrong-with-scriptmanager-registerstartupscript

My code:

ScriptManager.RegisterStartupScript(Me, upForm.GetType(), "AutoPostBackScript", "formSubmitPass();", True)

NOTE: Make sure to use ScriptManager NOT ClientScript, also for the second parameter use GetType() on the update panel.

Instead of using a stringbuilder ( http://stackoverflow.com/questions/789292/efficient-use-of-the-asp-net-registerstartupscript-method ) on the code behind, which I felt was unwieldy to put javaScript on the code behind, I just make the separate function and called that, which worked great

sah302