Gioce90
Gioce90

Reputation: 623

How call c# button event from JQuery Dialog

This was my precedent implementation:

In example.aspx:

<asp:Button ID="rejectButton" runat="server" Text="REJECT" OnClick="OnRejectButtonClick"/>

and in example.aspx.cs:

protected void OnRejectButtonClick(object sender, EventArgs e)
{
    // a lot of stuff...
    rejecter();
}

... this works.

Now I need a dialog for confirm the rejecting. Now my code is:

In example.aspx:

<asp:Button ID="rejectButton" runat="server" Text="REJECT" OnClientClick="return openDialog();"/>

<div id="dialog" title="Seleziona causa">
    <p>... bla bla...</p>
</div>

<script>
    $(function () {
        $("#dialog").dialog({ autoOpen: false, modal: true, show: "blind", hide: "blind",
            buttons: {
                "Okay": function () {
                    OnRejectButtonClick; // DOESN'T WORK!
                    $(this).dialog("close");

                },
                "Annulla": function () {
                    $(this).dialog("close");
                }
            }
        });
    });

    function openDialog() {
        $("#dialog").dialog("open");
        return false;
    }
</script>

and example.aspx.cs is not changed.

But after this changing, the call to OnRejectButtonClick event doesn't fire anymore. How can I fix this?

Upvotes: 1

Views: 787

Answers (3)

mybirthname
mybirthname

Reputation: 18127

You need to call it with jquery.

//before you click the button you should set a global variable
serverEx = true;
$("#rejectButton").click();

And in your OnClientSciprt method for the reject button openDialog

function openDialog() {

    if(serverEx)
       return true;

    $("#dialog").dialog("open");
    return false;
}

Add ClientIDMode="Static" to the button like Chris Davis said.

Upvotes: 2

Stephen Reindl
Stephen Reindl

Reputation: 5819

You could try something like this:

__doPostBack('rejectButton','OnClick'); 

to actually perform the asp.net action.

Upvotes: 0

Ayman Barhoum
Ayman Barhoum

Reputation: 1255

If you don't want to use jquery you can use this :

__doPostBack('<%= rejectButton.UniqueID %>', "");

so your code will be like this :

   $(function () {
        $("#dialog").dialog({ autoOpen: false, modal: true, show: "blind", hide: "blind",
            buttons: {
                "Okay": function () {
                    __doPostBack('<%= rejectButton.UniqueID %>', "");
                    $(this).dialog("close");

                },
                "Annulla": function () {
                    $(this).dialog("close");
                }
            }
        });
    });

Upvotes: 2

Related Questions