首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AddThis.com - ScriptManager

AddThis.com - ScriptManager
EN

Stack Overflow用户
提问于 2011-02-05 05:50:07
回答 1查看 1.2K关注 0票数 4

在从更新面板回发后,我正在尝试注册JS文件。我正在尝试让AddThis.com在回发后工作。如果我将multiview.activeviewindex设置为1,它就可以工作。但是,如果我从视图1转到视图2,它就不能工作。

以下是该项目的服务器端代码。

代码语言:javascript
复制
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'ScriptManager.RegisterStartupScript(Me, Me.GetType, "Test", "http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d4c6a5604aba88b", False)
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "Test1", "http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d4c6a5604aba88b", True)

MultiView1.ActiveViewIndex = 0
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    MultiView1.ActiveViewIndex = 1
End Sub

下面是ASPX代码:

代码语言:javascript
复制
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication6._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d4c6a5604aba88b"></script>

</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
                    <asp:View ID="View1" runat="server">
                        <asp:Button ID="Button1" runat="server" Text="Button" />
                    </asp:View>
                    <asp:View ID="View2" runat="server">
                    <!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
<a class="addthis_button_preferred_1"></a>
<a class="addthis_button_preferred_2"></a>
<a class="addthis_button_preferred_3"></a>
<a class="addthis_button_preferred_4"></a>
<a class="addthis_button_compact"></a>
</div>

<!-- AddThis Button END -->

                    </asp:View>
                </asp:MultiView>
            </ContentTemplate>
        </asp:UpdatePanel>

    </div>
    </form>
</body>
</html>

我已经尝试在scriptmanager上注册启动脚本。

有谁知道怎么让它工作吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2011-06-15 23:23:03

看起来AddThis只在窗口或文档加载期间起作用,因此这里需要一些小技巧。基本上的想法是,你应该在第一个视图上呈现javascript div,然后将其抓取到AddThis字段中,然后在第二个视图中提供的div中显示。

首先,您必须更改服务器端代码,因为您希望包含整个文件,而不是脚本块。你也应该只在PageLoad方法中这样做(我使用的是c#,所以你必须重写下面的调用)。

代码语言:javascript
复制
ScriptManager.RegisterClientScriptInclude(this, GetType(), "Test", "http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d4c6a5604aba88b");

你也可以把它放在你的HTML头中(就像你做的那样),然后去掉ScriptManager调用,在这种情况下这是不必要的。然后你必须稍微改变一下你的多视图的内容

代码语言:javascript
复制
<asp:View ID="View1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Button" />
    <div style="left:-2000em; position:absolute;">
        <div class="addthis_toolbox addthis_default_style addthis_32x32_style" id="addThis">
            <a class="addthis_button_preferred_1"></a>
            <a class="addthis_button_preferred_2"></a>
            <a class="addthis_button_preferred_3"></a>
            <a class="addthis_button_preferred_4"></a>
            <a class="addthis_button_compact"></a>
        </div>
    </div>
</asp:View>
<asp:View ID="View2" runat="server">                                
    <div id='addHere'></div>
</asp:View>

为此,我编写了一个简单的javascript模块来处理这个任务。

代码语言:javascript
复制
PageModules = {};
PageModules.AddThis = function ()
{
    var $addThis;

    function takeAddThis()
    {
        setTimeout(function ()
        {
            $addThis = $('#addThis');
            $addThis.remove();
        }, 100);
        Sys.Application.remove_load(takeAddThis);
    }

    function fixAddThis()
    {
        var $addHere = $('#addHere');
        if ($addHere.length)
        {
            $addHere.html($addThis);
        }
    }

    Sys.Application.add_load(fixAddThis);
    Sys.Application.add_load(takeAddThis);
} ();

请注意,PageModules只扮演一个名称空间角色,以避免弄乱全局作用域。AddThis模块是自初始化的,它是一种单例。此外,您还需要引用jQuery或修改内部方法体。我已经用额外的“隐藏”目录包装了你的AddThis目录,以便不向用户显示它。根据msdn引用加载所有脚本并创建所有对象后,将激发add_load方法。

http://msdn.microsoft.com/en-us/library/bb383829.aspx

我们需要takeAddThis只触发一次,这样我在第一次调用后就解除了这个方法的绑定。额外的超时将我们的逻辑转移到队列的末尾,这确保了文档逻辑将被正确执行(我也可以在这里使用来自jQuery的$( AddThis ).ready,但是我想保持一致。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4903310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档