Can Javascript disable an ASP.Net Control inside a UserControl?
Asked Answered
T

1

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.

Tiptop answered 10/6, 2019 at 16:15 Comment(1)
May I suggest trying to rework your code to use SignalR (codeproject.com/Articles/526876/…)? 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.Mani
W
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;
    }
}
Wisconsin answered 13/6, 2019 at 10:20 Comment(5)
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.Tiptop
If you want to reduce stress on the server stop using UpdatePanel and use a pure jQuery/Ajax solution to fetch updates.Wisconsin
@Tiptop - 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?Mani
VDWWD I agree with you, however that is NOT possible at this point in the project. @Mani 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.Tiptop
Updated my answer.Wisconsin

© 2022 - 2024 — McMap. All rights reserved.