回发后填充图像问题

发布于 2024-11-05 17:12:03 字数 2328 浏览 1 评论 0原文

我遇到以下问题:

单击按钮时我想填充图像源并在 ModalPopupExtender 内显示图像。 为了打开 ModalPopupExtender,我调用 __doPostBack ,这会导致 UpdatePanel 进行回发并显示其中包含图像的适当视图。

 <script src="jquery-1.5.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    window.pageLoad = function () {
        $addHandler($get('btn1'), 'click', function (e) {
            __doPostBack('pbcTest', '');
            // setTimeout("PopulateImage()", 100);
            PopulateImage();
            $find('<%=mpePopup.ClientID%>').show();
            e.preventDefault();
        });
    }

    function PopulateImage() {
        $(".testImage").attr("src", "6.jpg");
    }

</script>

<ajaxtoolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" CombineScripts="false">
    </ajaxtoolkit:ToolkitScriptManager>
    <button id="btn1"> Click</button>
    <asp:LinkButton ID="lbTest" runat="server" Text="show popup" Style="display: none;" />
    <ajaxtoolkit:ModalPopupExtender runat="server" ID="mpePopup" TargetControlID="lbTest"
        PopupControlID="pnlPopup" BackgroundCssClass="modalBackground" CancelControlID="lbClose" />
    <asp:Panel runat="server" ID="pnlPopup" CssClass="modalPopup">
        <asp:LinkButton runat="server" ID="lbClose">Close</asp:LinkButton>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:MultiView runat="server" ID="mvPopup">
                    <asp:View ID="View1" runat="server">
                        <img class="testImage" />
                    </asp:View>
                </asp:MultiView>
                <cs:PostBackControl runat="server" ID="pbcTest" OnCallBack="pbcTest_CallBack" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Panel>

 protected void pbcTest_CallBack(object sender, CallBackEventArgs e)
    {
        mvPopup.ActiveViewIndex = 0; 
    }

如果在 __doPostBack 之后我立即调用 PopulateImage(),图像是空的(尽管 PopulateImage() 被调用并且图像出现了一段时间,但消失后,可以在萤火虫中看到)。

但是如果我设置超时 setTimeout("PopulateImage()", 100); 图像就会显示出来。

造成这种行为的原因是什么?

I have the following problem:

On button click I want to populate an image source and show an image inside a ModalPopupExtender.
In order to open the ModalPopupExtender I call __doPostBack that cause UpdatePanel to do postback and show appropriate View with the image inside it.

 <script src="jquery-1.5.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    window.pageLoad = function () {
        $addHandler($get('btn1'), 'click', function (e) {
            __doPostBack('pbcTest', '');
            // setTimeout("PopulateImage()", 100);
            PopulateImage();
            $find('<%=mpePopup.ClientID%>').show();
            e.preventDefault();
        });
    }

    function PopulateImage() {
        $(".testImage").attr("src", "6.jpg");
    }

</script>

<ajaxtoolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" CombineScripts="false">
    </ajaxtoolkit:ToolkitScriptManager>
    <button id="btn1"> Click</button>
    <asp:LinkButton ID="lbTest" runat="server" Text="show popup" Style="display: none;" />
    <ajaxtoolkit:ModalPopupExtender runat="server" ID="mpePopup" TargetControlID="lbTest"
        PopupControlID="pnlPopup" BackgroundCssClass="modalBackground" CancelControlID="lbClose" />
    <asp:Panel runat="server" ID="pnlPopup" CssClass="modalPopup">
        <asp:LinkButton runat="server" ID="lbClose">Close</asp:LinkButton>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:MultiView runat="server" ID="mvPopup">
                    <asp:View ID="View1" runat="server">
                        <img class="testImage" />
                    </asp:View>
                </asp:MultiView>
                <cs:PostBackControl runat="server" ID="pbcTest" OnCallBack="pbcTest_CallBack" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Panel>

 protected void pbcTest_CallBack(object sender, CallBackEventArgs e)
    {
        mvPopup.ActiveViewIndex = 0; 
    }

If after __doPostBack I call immediately PopulateImage(), the image is empty (although PopulateImage() is called and the image appears for a while, but after that disappears, it's possible to see in firebug).

But if I put a timeout setTimeout("PopulateImage()", 100); image shows up.

What could be the cause of such behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2024-11-12 17:12:03

如果您希望始终在 UpdatePanel 完成刷新后调用 PopulateImage();,您应该通过注册要在 UpdatePanel 之后执行的函数来实现此目的> 已加载。加载页面时,您可以通过在 javascript 中执行以下操作来注册您的函数:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(PopulateImage);

我认为您可能遇到同步问题,在 PopulateImage()UpdatePanel 刷新> 代码已运行,因此它会替换它。

If you want to always call PopulateImage(); after the UpdatePanel has completed a refresh you should do it by registering the function to execute after the UpdatePanel has loaded. You can register your function by doing the following in javascript when your page is loaded:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(PopulateImage);

I think you might be experiencing a synchronization issue where you UpdatePanel refresh is happening AFTER your PopulateImage() code has run so it replaces it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文