从jQuery的对话框按钮事件点击按钮ASP.NET [英] Click ASP.NET button from jQuery dialog button event

查看:240
本文介绍了从jQuery的对话框按钮事件点击按钮ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下ASP.NET标记:

I have the following ASP.NET markup:

<div id="myForm" title="My Form" style="display: none">
    <span>You can see me!</span>
    <div style="display: none">
        <asp:Button ID="btnSave" runat="server" Text="Button"
            OnClick="btnSave_Click" />
    </div>
</div>

<!-- called by an element click event elsewhere -->
<script type="text/javascript" language="javascript">
    $("#myForm").dialog({
        modal: true,
        width: 500,
        height: 200,
        resizable: false,
        buttons: {
            "Cancel": function () {
                $(this).dialog("close");
            },
            "Save": function () {
                $(this).dialog("close");
                // I want to call btnSave_Click (by DOM-clicking the button?)
            }
        }
    });
    $("#myForm").parent().appendTo("form:first");
</script>

我试图让jQuery.dialog生成按钮来代替ASP.NET按钮进行回传。我应该怎么做才能使按钮做了提交,并调用 btnSave_Click 方法?

"Save": function () {
    $(this).dialog("close");
    document.getElementById("<%=btnSave.ClientID %>").click();
}

...工作,但,这是最好的解决办法?

... works but is this the best solution?

推荐答案

您可以使用btnSave.ClientId物业发出来的JavaScript您正在寻找该控件的ID,那么你可以做这样的事情。这是如果你想btnSave控制作为回传控制寄存器,在code触发单击事件背后。

You can use the btnSave.ClientId property to emit to the javascript the ID of the control you are looking for, then you can do something like this. This is if you want the btnSave control to register as a postback control, triggering the "Click" event in the code behind.

var myControl = document.getElementById('theclientidgoeshere')
myControl.click();

这一定会做到!

如果你想要做一个简单的回传,你可以做

If you want to do a simple postback you can do

this.document.form.submit();

但不会注册任何事件的按钮。

but that will NOT register any events for the button.

这是最好的?

有关.NET 3.5和更早的版本是唯一的真正的安全的方式获取客户端ID。与jQuery你可以做_btnSave部分ID匹配,但我发现,这是为将来修改太冒险了。

For .NET 3.5 and earlier it is the only "truly" safe ways to get the client ID. With jQuery you could do a partial id match on _btnSave, but I find that that is too risky for future modifications.

.NET 4.0确实引入了一个选项,可能的帮助。

.NET 4.0 does introduce an option that might help.

.NET 4.0 ClientIDMode属性

在.NET 4.0中,你有一个可选的的ClientIDMode 属性,将允许您选择的客户端ID是如何创建的。您可以使用predictable或静态选项,让你硬code按钮的ID到您的JavaScript。

In .NET 4.0 you have an optional ClientIDMode Property that will allow you to choose how the client id is created. You can use Predictable or Static options to allow you to hard-code the id of the button into your JavaScript.

这篇关于从jQuery的对话框按钮事件点击按钮ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆