【问题标题】:Refresh Visual webpart through Javascript in sharepoint 2010在 sharepoint 2010 中通过 Javascript 刷新 Visual webpart
【发布时间】:2013-04-29 14:45:37
【问题描述】:

我在主页中添加了可视化 webpart。任何人都可以为我提供 java 脚本以每 2 分钟刷新一次此 Webpart。

感谢您的宝贵时间。

问候, 桑迪

【问题讨论】:

    标签: sharepoint sharepoint-2010 refresh visual-web-developer-2010


    【解决方案1】:

    我相信您无需额外的 JavaScript 即可实现这一目标。如果您在 Web 部件中构建计时(使用计时器)会怎样?然后,您可以公开允许最终用户调整刷新间隔的 Web 部件属性。如果您不需要调整刷新间隔,那么您不需要公开该属性。希望这会有所帮助。

    示例 按照此link 了解如何在 Visual Web 部件中使用 ajax。然后执行以下操作

    ASCX 标记

    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:ListBox ID="ListBox1" runat="server" Height="177px" Width="269px"></asp:ListBox>
    
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
        </asp:UpdatePanel>
    
            <asp:Timer ID="Timer1" runat="server" Interval="10000" 
                onprerender="Timer1_PreRender" ontick="Timer1_Tick">
            </asp:Timer>
        </div>
    

    ASCX 代码隐藏

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    
    namespace vwpTestAjaxRefresh.VisualWebPart1
    {
        public partial class VisualWebPart1UserControl : UserControl
        {
            protected void Timer1_Tick(object sender, EventArgs e)
            {
                string currenttime = DateTime.Now.ToString("MM/dd/yy H:mm:ss zzz");
                ListBox1.Items.Add("Hello World at " + currenttime);
            }
        }
    }
    

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回复。您能给我一个在这种情况下使用计时器的 hello world 示例吗?
    • @sandy_9866141457 请参阅上面我最初回答的更新。添加了示例代码。
    • 再次感谢您的回复。如果您在上面看到我发布的代码,则与您所做的相同。我正在使用 java 脚本在页面上显示警报横幅。计时器没有关系。在上面的代码中,标签 DateStampLabel 正在刷新,但没有刷新其他标签 lblScripter。不知道为什么会发生这种情况,对此非常感谢。
    • 你同时做的事情太多了。只留下基本代码。不要做可见=真/假。做文本=“随便”/“”。删除所有 JavaScript。在做其他花哨的事情之前,请确保您的 timer_tick 代码正在运行。这样你就知道出了什么问题。
    • 我试过了,我的代码运行良好。但是我的标签,即 lblScripter 没有刷新???
    猜你喜欢
    • 2011-07-27
    • 2011-03-26
    • 2011-09-05
    • 2012-08-31
    • 2013-06-07
    • 1970-01-01
    • 2023-03-03
    • 2012-09-06
    • 2012-08-18
    相关资源
    最近更新 更多