SimpleModal breaks ASP.Net Postbacks

前端 未结 10 1932
名媛妹妹
名媛妹妹 2020-12-08 20:00

I\'m using jQuery and SimpleModal in an ASP.Net project to make some nice dialogs for a web app. Unfortunately, any buttons in a modal dialog can no longer execute their po

相关标签:
10条回答
  • 2020-12-08 20:30

    got caught out by this one - many thanks to tghw and all the other contributors on the appendto form instead of body fix. (resolved by attributes on the 1.3 version)

    btw: If anyone needs to close the dialog programmatically from .net - you can use this type of syntax

    private void CloseDialog()
    {
        string script = string.Format(@"closeDialog()");
        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
    }
    

    where the javascript of closedialog is like this....

        function closeDialog() {
            $.modal.close();
        }
    
    0 讨论(0)
  • 2020-12-08 20:30

    I have found the following works without modifying simplemodal.js:

    function modalShow(dialog) {
    
        // if the user clicks "Save" in dialog
        dialog.data.find('#ButtonSave').click(function(ev) {
            ev.preventDefault();
    
            //Perfom validation                
    
            // close the dialog
            $.modal.close();
    
            //Fire the click event of the hidden button to cause a postback
            dialog.data.find('#ButtonSaveTask').click();
        });
    
        dialog.data.find("#ButtonCancel").click(function(ev) {
            ev.preventDefault();
            $.modal.close();
        });
    }          
    

    So instead of using the buttons in the dialog to cause the postback you prevent their submit and then find a hidden button in the form and call its click event.

    0 讨论(0)
  • 2020-12-08 20:30

    FWIW, I've updated the blog post you pointed to with come clarification, reposted here - the reasoning & other details are in the blog post:

    The solution (as of my last checkin before lunch):

    1. Override the dialog's onClose event, and do the following:
      1. Call the dialog's default Close function
      2. Set the dialog div's innerHTML to a single  
      3. Hijack __doPostBack, pointing it to a new function, newDoPostBack

    From some comments I’ve seen on the web, point 1 needs some clarification.  Unfortunately, I’m no longer with the same employer, and don’t have access to the code I used, but I’ll do what I can.  First off, you need to override the dialog’s onClose function by defining a new function, and pointing your dialog to it, like this:

    $('#myJQselector').modal({onClose: mynewClose});
    • Call the dialog's default Close function.  In the function you define, you should first call the default functionality (a best practice for just about anything you override usually):
    • Set the dialog div's innerHTML to a single   – This is not a required step, so skip it if you don’t understand this.
    • Hijack __doPostBack, pointing it to a new function, newDoPostBack
    function myNewClose (dialog)
    {
        dialog.close();
        __doPostBack = newDoPostBack;
    }
    1. Write the newDoPostBack function:
    function newDoPostBack(eventTarget, eventArgument)
    {
        var theForm = document.forms[0];
        if (!theForm)
        {
            theForm = document.aspnetForm;
        }
     
        if (!theForm.onsubmit || (theForm.onsubmit() != false))
        {
            document.getElementById("__EVENTTARGET").value = eventTarget;
            document.getElementById("__EVENTARGUMENT").value = eventArgument;
            theForm.submit();
        }
    }
        
    0 讨论(0)
  • 2020-12-08 20:34

    if you don want modify the SimpleModal source. try this..

    After you call the modal() method add this:

    $("#simplemodal-overlay").appendTo('form');
    $("#simplemodal-container").appendTo('form');
    

    the SimpleModal plugin add two this to your markup.

    1. 'simplemodal-overlay' for the background
    2. 'simplemodal-container' containig the div that you whant as pop up modal.
    0 讨论(0)
  • 2020-12-08 20:44

    All standard ASP.NET postbacks work by calling a __doPostBack javascript method on the page. That function submits the form (ASP.NET only really likes one form per page) which includes some hidden input field in which all the viewstate and other goodness lives.

    On the face of it I can't see anything in SimpalModal that would screw up your page's form or any of the standard hidden inputs, unless the contents of that modal happened to come from a HTTP GET to an ASP.NET page. That would result in two ASP.NET forms being rendered into one DOM and would would almost certainly screw up the __doPostBack function.

    Have you considered using the ASP.NET AJAX ModalPopup control?

    0 讨论(0)
  • 2020-12-08 20:45

    The new Jquery.simplemodal-1.3.js has an option called appendTo. So add an option called appendTo:'form' because the default is appendTo:'body' which doesn't work in asp.net.

    0 讨论(0)
提交回复
热议问题