4

開くとすぐに閉じるjQueryダイアログボックスがあります。GridView のテンプレート フィールドにあるボタンに設定されます。

私のJavaScript:

<script type="text/javascript">
    $(document).ready(function() {
        $("#txtBeginDate").datepicker();
        $("#txtEndDate").datepicker();

        $("#response").dialog({
            autoOpen: false,
            modal: true,
            title: "Equifax Response"
        });

        $("[id*=lnkEquifaxResponse]").live("click", function EquifaxResopnse() {
            $("#response").dialog("open");
        });
    });
</script>

関連する GridView マークアップ:

<div id="Gridview">
    <asp:GridView ID="grClientTransactions" runat="server" AllowPaging="True" 
        PageSize="25" AutoGenerateColumns="False" DataKeyNames="ResponseXML"
        EmptyDataText="Record not found." EmptyDataRowStyle-BackColor="#CCCCCC" 
        EmptyDataRowStyle-Font-Bold="true" CssClass="mGrid" 
        PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" 
        OnPageIndexChanging="grClientTransactions_PageIndexChanging" 
        onrowcommand="grClientTransactions_RowCommand">

        <Columns>
            <asp:TemplateField ShowHeader="false">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkEquifaxResponse" runat="server" 
                        CausesValidation="False"
                        CommandName="EquifaxResponse" 
                        Text="View" 
                        CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'>
                    </asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField Visible="false" HeaderText="Equifax Response">
                <ItemTemplate>
                    <asp:Label ID="lblEquifaxResponse" runat="server" 
                        Text='<%# Bind("ResponseXML")%>' >
                    </asp:Label></div>                            
                </ItemTemplate>
            </asp:TemplateField>                          
        </Columns>

CodeBehind から割り当てられた文字列を含むラベルを表示する私の div:

<div id="response">
    <asp:Label ID="lblDialog" runat="server" ></asp:Label>
</div>
4

3 に答える 3

2

がポストバックを引き起こしている場合lnkEquifaxResponse、それが問題です。ダイアログは、ポストバック後に閉じたものとして再レンダリングされています。UpdatePanels などを使用していますか?

于 2013-03-28T16:28:38.610 に答える
2

jQuery のlive()メソッドはバージョン 1.9 で廃止され、削除され、on()メソッドに置き換えられました。

したがって、これを置き換えます。

$("[id*=lnkEquifaxResponse]").live("click", function EquifaxResopnse() {
    $("#response").dialog("open");
});

これとともに:

$("[id*=lnkEquifaxResponse]").on("click", function EquifaxResopnse() {
    $("#response").dialog("open");

    return false; // Prevents the postback
});

これは別の方法で行うことができます。

$(document).ready(function() {

    $("[id*=lnkEquifaxResponse]").on("click", function EquifaxResopnse() {
            $("#lblDialog").empty();
        });

    if($("#lblDialog").text() != "")
    {
        $("#response").dialog("open");
    }
});
于 2013-03-28T16:26:51.237 に答える
1

live メソッドは非推奨であり、代わりにこのようにする必要があります。コールバックに渡すイベント引数を追加する必要があります。preventDefault(); も追加する必要があります。これにより、アンカー タグのデフォルトの動作が妨げられます

  $("[id*=lnkEquifaxResponse]").on("click", function EquifaxResopnse(ev) {
        //prevents the default behavior of the anchor tag causing the page to postback or re-render
        ev.preventDefault();
        $("#response").dialog("open");
    });

代わりに、コールバックの最後に追加することもできます

return false;

しかし、これは IE では非常にバグが多くなり、ev.preventDefault() は IE をチェックし、return false または returnValue を追加するかどうかをチェックします。IE7 はこれを見るのが好きで、それ以外の場合は機能しません。

ev.returnValue = false;

preventDefault() のソースは、jQuery では次のようになります。

preventDefault: function() {
    this.isDefaultPrevented = returnTrue;

    var e = this.originalEvent;
    if ( !e ) {
        return;
    }

    // if preventDefault exists run it on the original event
    if ( e.preventDefault ) {
        e.preventDefault();

    // otherwise set the returnValue property of the original event to false (IE)
    } else {
        e.returnValue = false;
    }
},
于 2013-03-28T16:50:22.493 に答える