SimpleModal breaks ASP.Net Postbacks

2020-02-03 06:11发布

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 postbacks, which is not really acceptable.

There is one source I've found with a workaround, but for the life of me I can't get it to work, mostly because I am not fully understanding all of the necessary steps.

I also have a workaround, which is to replace the postbacks, but it's ugly and probably not the most reliable. I would really like to make the postbacks work again. Any ideas?

UPDATE: I should clarify, the postbacks are not working because the Javascript used to execute the post backs has broken in some way, so nothing happens at all when the button is clicked.

10条回答
仙女界的扛把子
2楼-- · 2020-02-03 06:40

In addition to tghw's answer, this excellent blog post helped me: jQuery: Fix your postbacks in Modal forms -- specifically BtnMike's comment: "You also must not have CssClass=”simplemodal-close” set on your asp:button." Taking that off the class was the not-obvious-to-me solution.

-John

查看更多
萌系小妹纸
3楼-- · 2020-02-03 06:42

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.

查看更多
叛逆
4楼-- · 2020-02-03 06:43

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.
查看更多
仙女界的扛把子
5楼-- · 2020-02-03 06:44

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();
    }
查看更多
冷血范
6楼-- · 2020-02-03 06:46

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.

查看更多
Root(大扎)
7楼-- · 2020-02-03 06:51

Had the same problem, but {appendTo:'form'} caused the modal popup to be rendered completely wrong (as though I had a CSS issue).

Turns out the template I'm building on top of has includes that put other forms on the page. Once I set {appendTo:'#aspnetForm'} (the default Asp.net form ID), everything worked great (including the postback).

查看更多
登录 后发表回答