当用户在c#代码后面的jquery对话框上单击“是”时执行操作

时间:2012-10-14 11:07:40

标签: c# jquery asp.net jquery-ui-dialog

现在我在asp.net中使用了一个消息框但是我想使用jquery用于相同的

        var DialogResult = MessageBox.Show("Do you want to create the File ?", "Start Invoicing", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (DialogResult == DialogResult.Yes)
        {
           //perform some action
        }
        else
        {
         //this
        }

这可以通过jquery来实现吗?可以根据我想要执行的内容更改jquery框的文本

请帮忙

 <script type="text/javascript">
    $(document).ready(function () {
        $('#cmdShowDialog').click(function (event) {
            event.preventDefault();
            var $dialog = $('<div>Dynamic Dialog with Buttons.</div>')
            .dialog
            ({
                title: 'Cancel',
                width: 300,
                height: 200,
                buttons:
                [{
                    text: "Yes",
                    click: function () {
                        $dialog.dialog('close');
                    }
                },
                {
                    text: "Cancel",
                    click: function () {
                        $dialog.dialog('close');
                    }
                }]
            });
        });
    });
</script>

   <asp:Button ID="cmdShowDialog" runat="server" Text="Show Dialog" />

尝试过使用此功能,但我该如何继续...

1 个答案:

答案 0 :(得分:3)

你可以做这样的事......
的javascript:

<script type="text/javascript">
    function dialogYesNoCancel(btn1func, btn2func) {
        $("#dialogMessage").dialog({
            resizable: false,
            height: 140,
            modal: true,
            buttons: {
                Cancel: function () {

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

                    $(this).dialog(btn2func());
                    $(this).dialog("close");

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

    function func1() {
        $("#Button1").click();
    }

    function func2() {
        $("#Button2").click();
    }
</script>

ASPX:

<asp:Button ID="Button1" clientidmode="Static" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" clientidmode="Static" runat="server" Text="Button" onclick="Button2_Click" />

CS:

protected void Page_Load(object sender, EventArgs e)
    {       
        ScriptManager.RegisterStartupScript(this, this.Page.GetType(), Guid.NewGuid().ToString(), "dialogYesNoCancel(func1, func2)", true);
    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        //do something
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        //do something
    }