简体   繁体   English

回传时会再次出现jQuery对话框

[英]jquery Dialog reappears on postback

I have a GridView with a TemplateField holding a LinkButton. 我有一个GridView,其中的TemplateField包含一个LinkBut​​ton。 When this button is clicked it converts some XML that is in another hidden TemplateField to a string and assigns it to a label in a hidden div to be used in a modal dialog. 单击此按钮时,它将另一个隐藏的TemplateField中的一些XML转换为字符串,并将其分配给隐藏的div中的标签以在模式对话框中使用。 Currently the dialog opens as expected with the proper information, it closes and I can select another record in the grid to view with no problems. 当前,该对话框将按预期方式打开并提供适当的信息,然后关闭,我可以选择网格中的另一条记录进行查看,不会出现任何问题。 This is where it goes wrong and I have not been able to find a solution. 这是哪里出错了,我一直找不到解决方法。 If I do something else on the page that causes a postback, the last dialog box to be opened will open on the new page load. 如果我在页面上执行其他操作导致回发,则在新页面加载时将打开最后一个打开的对话框。

Here is my relevant mark-up: 这是我的相关标记:

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

    $("#response").dialog({
        autoOpen: false,
        modal: true,
        height: "auto",
        width: "auto",
        title: "Equifax Response",
        close: function(ev, ui) {
            $(this).dialog('destroy').remove();
        }
    });

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

    if ($("#lblDialog").text() != "") {
        $("#response").dialog("open");
    }
});
    </script>

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

    <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" 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>
            <PagerStyle CssClass="pgr" />
            <AlternatingRowStyle CssClass="alt" />
        </asp:GridView>
    </div>

I am new to programming so any help would be greatly appreciated. 我是编程新手,所以将不胜感激。

Update your code like this: 像这样更新您的代码:

 <div id="response" visible="false">
    <asp:Label ID="lblDialog" runat="server" EnableViewState="False"></asp:Label>
 </div>

As far as I understand, when you click on a link, lblDialog is filled with some data on server and because of viewstate is enabled that data is restored on each postback. 据我了解,当您单击链接时,lblDialog将在服务器上填充一些数据,并且由于启用了viewstate,因此每次回发都将还原数据。 Than, if shown below returns true and popup is appears. 然后, if如下所示返回true,则出现弹出窗口。

if ($("#lblDialog").text() != "") {
    $("#response").dialog("open");
}

If you will add enableviewstate="false", inner text of lblDialog will be lost on second postback and lable will be empty after page is loaded (if not a link in grid is clicked). 如果添加enableviewstate =“ false”,则lblDialog的内部文本将在第二次回发时丢失,并且在加载页面后lable将为空(如果未单击网格中的链接)。

You can get more info about viewstate and how it works here 您可以在此处获取有关viewstate及其工作方式的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM