简体   繁体   中英

jQuery modal dialog and ASP.NET postback

I have a modal dialog on my page using jQuery that a user enters a password into.

It's a standard jQuery dialog and it works fine. I have linkbuttons in a datagrid that open the dialog using this code:

        $('.needsVal').click(function () {
            $("#login1").dialog('open');
            id = $(this).attr('id');
            return false;
        });

The problem is later on in the page I make an Ajax call, and based on the value returned, I want to selectively fire a postback for the page. The problem is the postback never fires. My postback code is as follows:

        if (returnValue == "true") {
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(id, "", false, "", "", false, true));
            return true;
        }
        else {
            alert("Authentication failure!\nPlease check your password and try again.");
            return false;

For some reason I can't get the postback to work with the modal dialog, and it's driving me nuts. I had this working with a regular Javascript prompt, but I had to change it because there's no way to mask the password in a prompt.

Any thoughts on how to get the postback to work?

id is a global variable that has the unique ID of the clicked button. I've confirmed that's being passed properly.

I managed to get this to work.

What I found was there are two ASP.NET controls on the page - two gridviews, one with regular linkbuttons and another with a linkColumn.

Because of the way the linkbuttons work in the two types of controls (linkbutton vs. commandbutton) I had to vary how I open the form, and how I interact with the prompt. I had to create two different events. (Maybe there's a way to do it with one, but I couldn't figure it out)

What I finally wound up with jQuery wise:

//Click event for gridview 1 (standard linkbutton)
$('.needsVal').click(function () {
                $("#login1").dialog('open');
                id = $(this).attr('id');
                return false;
            });

//Click event for second gridview (command button)
            $('.needsValSideMenu').click(function () {
                var h = $(this).html();
                script = h.substring(h.indexOf("\"") + 1, h.indexOf("\">"));
                $("#login2").dialog('open');
                return false;
            });

//AJAX call for standard linkbutton grid
function checkPassword() {
            var returnValue;
            $.ajax({
                async: false,
                type: "POST",
                url: "myservice.asmx/Authenticate",
                data: { "password": pw.value },
                dataType: "xml",
                error: function () { alert("Unexpected Error!"); },
                success: function (msg) {
                    returnValue = $(msg).find('boolean').text()
                }
            });
            if (returnValue == "true") {
                //alert(id.replace("_", "$"));
                id = id.split("_").join("$");                
                WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(id.replace("_","$"), "", true, "", "", false, true));
            }
            else {
                alert("Authentication failure!\nPlease check your password and try again.");
            }
        }

//Call for second grid (command button) - need to get and execute the script associated with this button
        function checkPasswordSideMenu() {
            var returnValue;
            $.ajax({
                async: false,
                type: "POST",
                url: "myservice.asmx/Authenticate",
                data: { "password": pw2.value },
                dataType: "xml",
                error: function () { alert("Unexpected Error!"); },
                success: function (msg) {
                    returnValue = $(msg).find('boolean').text()
                }
            });
            if (returnValue == "true") {
                eval(script);
            }
            else {
                alert("Authentication failure!\nPlease check your password and try again.");
            }
        }

I couldn't think of a way to do this in one method since I need to call a different checkPassword routine depending on which type of button was clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM