Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have made a custom Visual Webpart. I'm wanting to periodically update a label in the webpart without refreshing the whole page. I've got the label to update, however it is also updating the replies on the page that I've placed the webpart. The page I'm wanting to use it is in a Discussion Board on the Topic pages that contain the topic and all replies.

In my ascx file I have...

<asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger  ControlID="Timer1" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
        <asp:Label ID="LabelRefreshNotice" runat="server" Text="not refreshed yet."></asp:Label><br />
        <br />
    </ContentTemplate>
</asp:UpdatePanel>

<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="10000" />

And in my ascx.cs file...

protected void Timer1_Tick(object sender, EventArgs e)
{
    LabelRefreshNotice.Text = "Refreshed at: " + DateTime.Now.ToLongTimeString();
}

This updates the label like I want, but also updates all the replies and clears any text I have written in the reply box, which I don't want. How can I get it to update only the specific content in the web part?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I shouldn't be using UpdatePanel, as it posts back to the same page, and the replies are likely in the page-load event, hence are getting reloaded along with my label.

Instead I should be using something like this:

<SharePoint:ScriptLink ID="scriptlink" Name="SP.js" runat="server" OnDemand="true"
    Localizable="false" />
<script type="text/javascript">
    function getWebProperties() {
        var ctx = new SP.ClientContext.get_current();
        this.web = ctx.get_web();
        ctx.load(this.web);
        ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
            Function.createDelegate(this, this.onFail));
    }
    function onSuccess(sender, args) {
        var label = document.getElementById('<%=lblSiteTitle.ClientID %>');

        label.innerHTML = 'web title:' + this.web.get_title() + '\n ID:' + this.web.get_id() +
            '\n Created Date:' + this.web.get_created() +
            '\n Method Executed:' + new Date().getTime();
    }
    function onFail(sender, args) {
        label.value = 'failed to get list. Error:' + args.get_message();
    }

    function timerJob() {
        window.setInterval(function(){getWebProperties()}, 5000);
    }

    _spBodyOnLoadFunctionNames.push("timerJob");
</script>
<asp:Label ID="lblSiteTitle" runat="server" Text="Site info......" Width="100%"></asp:Label>
<br />
<asp:TextBox ID="txtFreeInput" TextMode="MultiLine" runat="server" Rows="4"></asp:TextBox>

Answer thanks to Borislav and copied from his responses in this post.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.