如何调用数据列表中按钮的asp回发

本文关键字:按钮 asp 回发 列表 数据 何调用 调用 | 更新日期: 2023-09-27 18:28:07

如何获取asp按钮的javascript回发事件调用?

    <ItemTemplate>
        <asp:Button id="btnTest" runat="server" 
                    Text="button" OnClientClick="
            customAsyncConfirmDialog(function (isconfirmed)
            {
                if(isconfirmed)
                {
                    //call this buttons postback event
                    <%# Page.ClientScript.GetPostBackEventReference(new PostBackOptions(Container.FindControl("btnTest")))%>;
                }
            });return false;
            "/>
    </ItemTemplate>
</asp:DataList>

更新:customAncConfirmDialog是一个异步函数,它不会阻止javascript等待响应。

如何调用数据列表中按钮的asp回发

不需要这么做。只要返回true,就会命中回发处理程序。例如:

<ItemTemplate>
    <asp:Button id="btnTest" runat="server" 
                Text="button" OnClientClick="
        customConfirmDialog(function (isconfirmed)
        {
            return isconfirmed;                
        });
        "/>
</ItemTemplate>

我很难访问我之前写的内容,但这里有一个基于你在另一个答案中写的内容的快速示例。你可能需要改变一些事情,但你应该明白:

window.confirm = function(question, ok, cancel) {
    var defer = $.Deferred();
    ok = ok || 'OK'; //setup text for the confirmation
    cancel = cancel || 'Cancel';
    $(document).ready(function() {
        $('#customMessage').text(question);
         var btnOk = $('#btnCustomOK');
         var btnCancel = $('#btnCustomCancel');
         btnOk.val(ok);
         btnCancel.val(cancel);
        btnOk.unbind("click"); //prevents chaining of events if multiple confirms are used
        btnCancel.unbind("click"); 
        btnOk.click(function() {
            $('#MyConfirm').hide();
            defer.resolve(true); //gets passed as isconfirmed=true
            return false; //prevent postback
        });
        btnCancel.click(function() {
            $('#MyConfirm').hide();
            defer.resolve(false);
            return false; //prevents postback
        });        
        $('#MyConfirm').show();                
        btnOk.focus();
    });
    return defer.promise();
}

然后使用:

<asp:Button id="btnTest" runat="server" Text="button"
  OnClientClick="confirm('My Message blah blah').then(//this runs after ok/cancel is clicked, thanks to defer.resolve() and defer.promise().
function(isConfirmed) {
 if (isConfirmed)
 __doPostBack(source.id,'');
}); return false;//return false here to prevent the initial postback from occurring"/>
    </ItemTemplate>

"source.id"部分并不完全正确,但包含承诺的部分应该会帮助您。

我能够用以下命令调用回发:

<script type="text/javascript">
    function customConfirmDialog(sourceButton, callback) {
        setTimeout(function () { callback(sourceButton, true); }, 1);
        return false;
    }
</script>
<asp:DataList ID="dlTest" runat="server">
    <ItemTemplate>
        <%# Page.ClientScript.GetPostBackEventReference(new PostBackOptions(Container.FindControl("btnTest")))%>;
        <asp:LinkButton runat="server" ID="neededfor__dopostbackgenerationforsomereason"></asp:LinkButton>
        <asp:Button id="btnTest" runat="server" 
                    Text="button"
            OnClick="btnTest_OnClick"
                    OnClientClick="
                return customConfirmDialog(this,function (sourceButton, isconfirmed)
                {
                    if(isconfirmed)
                    {
                        //call this button's postback event
                        __doPostBack(sourceButton.name,'');
                    }
                }); 
            "/>
    </ItemTemplate>
</asp:DataList>

我不得不添加一个链接按钮来生成__doPostBack,不知道为什么ASP:Button本身并没有触发它。我仍然无法在onClientClick函数中使用Page.ClientScript.GetPostBackEventReference

最后注意,您需要用sourceButton.name而不是sourceButton.id调用__doPostBack来触发正确的服务器端事件。