Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 316
Landmine Avatar asked Jun 10 '19 16:06

Landmine


1 Answers

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;
    }
}
like image 117
VDWWD Avatar answered Sep 21 '22 10:09

VDWWD