7

Can Javascript disable an ASP.net control inside a UserControl? My Javascript file is called from the MasterPage, I've also tried it from the ContentPage.

I'm attempting to use Javascript as an idle timer to reduce server postbacks without logging the users out. When the user goes back to focus on the window the timer restarts and refreshes again until the user is idle for 30 minutes and it pauses the ASP.Net timers.

Javascript:

var timer = $find('<%= TimerAutoRefresh.ClientID %>');
if (timer) {
    //stop the timer
    timer._stopTimer();
    timer.set_enabled(false);
    console.log("Timer disabled: " + timer);
}

ASP.net ASCX

<asp:UpdatePanel ID="UpdatePanel" runat="server">
    <ContentTemplate>
        <asp:Timer ID="TimerAutoRefresh" runat="server" Interval="5000" Enabled="true"></asp:Timer>
<asp:Literal ID="LiteralTest" runat="server">Timestamp</asp:Literal>
    </ContentTemplate>
</asp:UpdatePanel>

I've tried $find and document.getElementById and have not been able to get the ID. When I use ClientIDMode="Static" the page fails to refresh every 5 seconds as it does a complete postback.

Landmine
  • 1,759
  • 6
  • 39
  • 60
  • May I suggest trying to rework your code to use SignalR (https://www.codeproject.com/Articles/526876/Adding-SignalR-to-an-ASP-NET-WebForms-Project)? It will open a socket to the user, will update all data that is needed and can be set up to disconnect after a certain time. I know it mean a big rework but it might be worth it. Other suggestion is to make the update panel smaller make sure you send only the very needed data (numbers or string) and not a big chuck of html and css also. – theCuriousOne Jun 20 '19 at 08:37

1 Answers1

4

First, using an UpdatePanel will still cause the page to do a full PostBack, but only the updated parts are send to the browser again, so it will not reduce server load. Second, you cannot access a Timer Control like that.

But why not make it much simpler? If you really want to keep users logged in then why not do an Ajax request to some url to keep the session alive.

<script>
    var timer;
    var interval = 2500;

    function startTimer() {
        timer = setInterval(function () {
            $.ajax({
                type: 'GET',
                url: 'Default.aspx'
            });
        }, interval); 
    }

    function stopTimer() {
        clearInterval(timer);
    }

    startTimer();
</script>

More on jQuery timers here: start & stop / pause setInterval with javascript, jQuery

But you can also increase the timeout it takes for apsnet to logout users.

how to increase session timeout in asp.net?

Or login users with a persistent cookie. Usually when logging in you would see a checkbox "keep me logged in" or "remember me" or something similair.

How to create persistent cookies in asp.net?

UPDATE

Updating 500kb per user every 5 seconds seems like a bad idea, with or without UpdatePanels. But here is a way to stop the timer from the front-end.

<asp:UpdatePanel ID="UpdatePanel" runat="server">
    <ContentTemplate>
        <asp:Timer ID="TimerAutoRefresh" runat="server" Interval="5000" Enabled="true" OnTick="TimerAutoRefresh_Tick"></asp:Timer>
        <asp:Literal ID="LiteralTest" runat="server">Timestamp</asp:Literal>
        <asp:HiddenField ID="HiddenField1" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

<button id="StopTimer">Stop Timer</button>

<script>
    $('#StopTimer').bind('click', function (e) {
        $('#<%= HiddenField1.ClientID %>').val('Stop!');
        e.preventDefault();
    });
</script>

And then in code behind

protected void TimerAutoRefresh_Tick(object sender, EventArgs e)
{
    LiteralTest.Text = DateTime.Now.ToString();

    if (HiddenField1.Value == "Stop!")
    {
        LiteralTest.Text = "Stopped";
        TimerAutoRefresh.Enabled = false;
    }
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • I dont want to keep them logged in really, just want them to stop getting updates if they are idle to reduce stress on the server. – Landmine Jun 18 '19 at 02:50
  • 3
    If you want to reduce stress on the server stop using UpdatePanel and use a pure jQuery/Ajax solution to fetch updates. – VDWWD Jun 18 '19 at 06:23
  • @Landmine - can you explain how exactly does the "idle user" causes stress to the server? I find it a little bit strange that you receive postback while the user is idle. Or maybe I haven't understood the issue correctly? – theCuriousOne Jun 19 '19 at 14:39
  • VDWWD I agree with you, however that is NOT possible at this point in the project. @theCuriousOne The UpdatePanels are on timers that are refreshing data every 5 seconds. Because the UpdatePanels as VDWWD hinted at, are less than great, they are generating a lot of postbacks and refreshing ~500KB of data every 5 seconds per user. – Landmine Jun 19 '19 at 21:39
  • Updated my answer. – VDWWD Jun 19 '19 at 22:11