ASP.NET HoverMenuExtender 延迟加载

发布于 2024-07-21 17:54:48 字数 2948 浏览 7 评论 0原文

我正在尝试让我的悬停菜单扩展程序执行一些延迟加载。 我在整个网站上都有头像,当将鼠标悬停在上面时,应该会拉回各种内容(图像、最近的帖子、帖子计数等)出于明显的原因,我不想对 page_load 上的所有头像执行此操作。

使用以下代码,我能够获取悬停事件以异步触发到服务器的回发(在鼠标悬停时命中断点)。 但是,执行完成后,回发中的命令似乎没有反映出来。 加载图像/标签保留在悬停面板中。 任何帮助表示赞赏!

编辑:我刚刚意识到页面上渲染的最后一个头像可以正常工作,但上面的头像都不能正常工作。 有什么想法可能导致这种奇怪的行为吗?

<script language="javascript" type="text/javascript">
    function OnHover(image) {        
        __doPostBack('<%= this.imageHoverTrigger.UniqueID %>', '');
    }
</script>

<!-- DUMMY Hover Trigger -->
<input id="imageHoverTrigger" runat="server" style="display:none;"
   type="button" onserverclick="imageHoverTrigger_Click" />

<!-- User Avatar -->
<div style="border: solid 1px #AAA; padding:2px; background-color:#fff;">    
    <asp:ImageButton ID="UserImg" runat="server" />  
</div>         

 <!-- Hover tooltip disabled by default 
    (Explicitly enabled if needed)-->
<ajax:HoverMenuExtender ID="UserInfoHoverMenu" Enabled="false" runat="server"
    OffsetX="-1"
    OffsetY="3" 
    TargetControlID="UserImg"
    PopupControlID="UserInfoPanel" dyn
    HoverCssClass="userInfoHover"
    PopupPosition="Bottom">
</ajax:HoverMenuExtender>

 <!-- User Profile Info -->
<asp:Panel ID="UserInfoHover" runat="server" CssClass="userInfoPopupMenu">            
    <asp:UpdatePanel ID="UserInfoUpdatePanel" runat="server" UpdateMode="Conditional" >
        <ContentTemplate> 
            <asp:Image ID="loadingImg" runat="server" ImageUrl="~/Design/images/ajax-loader-transp.gif" />                              
            <asp:Label ID="loadingLbl" runat="server" Text="LOADING..." ></asp:Label>  
            <asp:Panel ID="UserInfo" runat="server" Visible="false">
                <b><asp:Label ID="UserNameLbl" runat="server"></asp:Label><br /></b>  
                <span style="font-size:.8em">
                    <asp:Label ID="UserCityLbl" runat="server" Visible="false"></asp:Label> <asp:Label ID="UserStateLbl" runat="server" Visible="false"></asp:Label>
                </span>
            </asp:Panel>                        
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="imageHoverTrigger" />
        </Triggers>                    
    </asp:UpdatePanel>     
</asp:Panel>

以及隐藏代码:

protected void Page_Load(object sender, EventArgs e)
{
    UserImg.Attributes.Add("onmouseover", "javascript:OnHover(this)");
}

protected void imageHoverTrigger_Click(object sender, EventArgs args)
{
    // Hide loading image/label
    loadingLbl.Visible = false;        
    loadingImg.Visible = false;

    //TODO: Set user data here
    UserInfo.Visible = true;
}

I'm trying to get my hovermenuextenders to do some lazy loading. I have avatars across the site that when hovered over should pull back various things (images, recent posts, post count, etc) For obvious reasons I don't want to do this for all avatars on the page_load.

Using the following code I'm able to get the hover event to trigger a postback to the server asynchronously (breakpoint is hit onmouseover). However, the commands in the postback don't seem to be reflected after execution is done. The loading image/label stay in the hover panel. Any help is appreciated!

EDIT: I just realized that the very last avatar rendered on the page works properly but none of the ones above it do. Any ideas what might be causing this strange behavior?

<script language="javascript" type="text/javascript">
    function OnHover(image) {        
        __doPostBack('<%= this.imageHoverTrigger.UniqueID %>', '');
    }
</script>

<!-- DUMMY Hover Trigger -->
<input id="imageHoverTrigger" runat="server" style="display:none;"
   type="button" onserverclick="imageHoverTrigger_Click" />

<!-- User Avatar -->
<div style="border: solid 1px #AAA; padding:2px; background-color:#fff;">    
    <asp:ImageButton ID="UserImg" runat="server" />  
</div>         

 <!-- Hover tooltip disabled by default 
    (Explicitly enabled if needed)-->
<ajax:HoverMenuExtender ID="UserInfoHoverMenu" Enabled="false" runat="server"
    OffsetX="-1"
    OffsetY="3" 
    TargetControlID="UserImg"
    PopupControlID="UserInfoPanel" dyn
    HoverCssClass="userInfoHover"
    PopupPosition="Bottom">
</ajax:HoverMenuExtender>

 <!-- User Profile Info -->
<asp:Panel ID="UserInfoHover" runat="server" CssClass="userInfoPopupMenu">            
    <asp:UpdatePanel ID="UserInfoUpdatePanel" runat="server" UpdateMode="Conditional" >
        <ContentTemplate> 
            <asp:Image ID="loadingImg" runat="server" ImageUrl="~/Design/images/ajax-loader-transp.gif" />                              
            <asp:Label ID="loadingLbl" runat="server" Text="LOADING..." ></asp:Label>  
            <asp:Panel ID="UserInfo" runat="server" Visible="false">
                <b><asp:Label ID="UserNameLbl" runat="server"></asp:Label><br /></b>  
                <span style="font-size:.8em">
                    <asp:Label ID="UserCityLbl" runat="server" Visible="false"></asp:Label> <asp:Label ID="UserStateLbl" runat="server" Visible="false"></asp:Label>
                </span>
            </asp:Panel>                        
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="imageHoverTrigger" />
        </Triggers>                    
    </asp:UpdatePanel>     
</asp:Panel>

And the code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    UserImg.Attributes.Add("onmouseover", "javascript:OnHover(this)");
}

protected void imageHoverTrigger_Click(object sender, EventArgs args)
{
    // Hide loading image/label
    loadingLbl.Visible = false;        
    loadingImg.Visible = false;

    //TODO: Set user data here
    UserInfo.Visible = true;
}

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

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

发布评论

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

评论(1

萌能量女王 2024-07-28 17:54:48

想通了:

我的 Page_Load 事件连接应该是:

    UserImg.Attributes.Add("onmouseover", "javascript:OnHover('" + this.imageHoverTrigger.UniqueID + "','" + this.hiddenLbl.ClientID + "')");
    UserImg.Attributes.Add("onmouseout", "javascript:ClearTimer()");

并且 javascript 函数应该是:

var hoverTimer;

    // Called on the hover of the user image
    function OnHover(trigger, hiddenTxt) {

        var field = document.getElementById(hiddenTxt);

        // Only post if this hover hasn't been done before
        if (field == null || field.innerHTML == "false") {
            hoverTimer = setTimeout(function() { ShowInfo(trigger) }, 500);          
        }            
    }

    // Clears timeout onmouseout
    function ClearTimer() {
        clearTimeout(hoverTimer);
    }

    // Retrieves user info from server
    function ShowInfo(trigger) {
        __doPostBack(trigger, '');
    }

我还在表单上添加了一个隐藏字段,以便知道何时执行悬停。 后面的代码将隐藏字段设置为 true,并且我的 JavaScript 每次执行时都会检查隐藏字段的值。 每次用户将鼠标悬停在图像上时,这都会阻止代码进行往返。

Figured it out:

My Page_Load event hookup should've been:

    UserImg.Attributes.Add("onmouseover", "javascript:OnHover('" + this.imageHoverTrigger.UniqueID + "','" + this.hiddenLbl.ClientID + "')");
    UserImg.Attributes.Add("onmouseout", "javascript:ClearTimer()");

and the javascript function should've been:

var hoverTimer;

    // Called on the hover of the user image
    function OnHover(trigger, hiddenTxt) {

        var field = document.getElementById(hiddenTxt);

        // Only post if this hover hasn't been done before
        if (field == null || field.innerHTML == "false") {
            hoverTimer = setTimeout(function() { ShowInfo(trigger) }, 500);          
        }            
    }

    // Clears timeout onmouseout
    function ClearTimer() {
        clearTimeout(hoverTimer);
    }

    // Retrieves user info from server
    function ShowInfo(trigger) {
        __doPostBack(trigger, '');
    }

I also added a hidden field on the form in order to know when the hover has been executed. The code behind sets the hidden field to true and my javascript checks for the value of the hidden field each time it executes. This stops the code from doing round trips each time the user hovers over the image.

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