Avoid post back while doing a timer tick function

試著忘記壹切 提交于 2020-01-03 16:44:48

问题


i need to do update images in my site without doing post back in a page.So, i loaded the images in ad-rotator to toggl between images.

I also used the timer tick function to refresh the images in the ad-rotator.

But while timer tick function refresh the whole page is refreshed so that all functions that i declared in the page is reloaded.

i need to only images in the ad-rotator images should refresh not the whole page.

i need to avoid whole refresh of the page. Pls help me.

$(document).ready(function() {
    setInterval(function() {
        $('#<% = addrotat.ClientID %>').load(location.href + ' #<% = addrotat.ClientID %> ', '' + Math.random() + '');
    }, 5000);
});

回答1:


You need to use ajax, by using Update panels with proper triggers, update panel Enables sections of a page to be partially rendered without a postback.

<asp:UpdatePanel ID="up1" runat="server">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="yourControl" EventName="yourControlEvent" />
        </Triggers>
        <ContentTemplate> your  content goes here       </ContentTemplate>

    </asp:UpdatePanel>    

you can check this links

update panel, with Triggers,

Updatepanel documentation

example for adrotator without refreshing the page




回答2:


//auto refresh clock without postback

<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <script type="text/vbscript" runat ="server" >
                Private Sub fun()
                    label.Text = Now.ToString
                End Sub
            </script>
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="up1" runat="server">
                <ContentTemplate ></ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="tim" EventName="Tick" />
                </Triggers>
                <ContentTemplate>  
                    <asp:Timer ID="tim" Interval ="1000" Enabled ="true " OnTick ="fun" runat ="server" ></asp:Timer>
                    <asp:Label ID="label" runat ="server" Text="DD/MM/YY"></asp:Label>     
                </ContentTemplate>
            </asp:UpdatePanel>  
        </div>
    </form>
</body>


来源:https://stackoverflow.com/questions/9063045/avoid-post-back-while-doing-a-timer-tick-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!