Timer client-side script is malfunctioning

While diving into the realm of Asp.net Ajax, I decided to experiment with calling javascript from my C# script.

I created a timer that triggers a method to execute a basic javascript alert function and update the time every 10 seconds. Even though my code seems to be error-free and exception-free, it's simply not working. The C# part successfully updates the time, but the javascript fails to execute the alert.

    <%@ Page Language="C#" %>

    <!DOCTYPE html>
    <html>

    <head runat="server">
        <title></title>
        <script runat="server">
        protected void Page_load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Label1.Text = DateTime.Now.ToString();
            }
        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            const string someScript = "alertMe";
            Label1.Text = DateTime.Now.ToString();
            ClientScript.RegisterStartupScript(this.GetType(),someScript, "alert('I was called from Content page!')", true);
        }
    </script>

    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager runat="server"> </asp:ScriptManager>
     <asp:UpdatePanel runat="server">
         <ContentTemplate>
             <asp:Label runat="server" ID="Label1"></asp:Label>
             <asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick ="Timer1_Tick"></asp:Timer>
         </ContentTemplate>
     </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>

Answer №1

When working with an UpdatePanel and placing the element that triggers a postback inside it, it is recommended to use ScriptManager instead of ClientScript.

Update your code to the following:

ScriptManager.RegisterStartupScript(this, this.GetType(), someScript, @"alert('I was called from Content page!');", true);

Answer №2

Don't forget to include a semicolon at the end of your statement;

ClientScript.RegisterStartupScript(this.GetType(),someScript, "alert('I was called from Content page!');", true);

Check your browser's error console to catch these issues in real-time.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to sum up the values in my object without having to use Object.entries within?

Here is a snippet from my JSON data: const jsonData = { "08/23/2022": { "One": 254, "Two": 92, "Three": 8 }, "08/13/2022": { "One": 327, "Two": 86, }, " ...

unable to disable custom field component in Angular Material

I've built a unique slide toggle feature using Angular Material. The steps I followed to create it can be found here: https://material.angular.io/guide/creating-a-custom-form-field-control While everything works smoothly, there's an issue when I ...

The second function in Vue.js was unable to initialize the data field within data() due to a missing call for assistance

I have very little experience working with vue js. There are two functions that I am using: loadComponentsOfUser() and loadUserId(). The loadComponentsOfUser() function depends on the userID field being loaded by the loadUserId() function. data() { retu ...

Node React authentication

Struggling with implementing authentication in React Router. I am using componentDidMount to check if the user is logged in by calling an endpoint. If not, it should redirect to login, otherwise proceed to the desired component. However, this setup doesn&a ...

Display/Conceal WP Submenu

Struggling to switch the behavior of my Wordpress menu. Want it to display when clicked, not when hovered: <nav> <ul> <li> <?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ...

Neglecting asynchronous Ajax functions once they have been called

I currently have a function that looks like this: $("#transfer").click(function(){ var token_rec = $("#token_rec").val(); var user_rec = $("#user_rec").val(); var subdomain_rec = $("#subdominio_rec").val(); var req_rec ...

Ways to stop the default action in a confirm dialog while using Angular JS

Within my save function, I have the following: $scope.saveData = function () { if (confirm("Are you sure you want to save") === false) { return } // do saving When using the above code and clicking "yes," I encounter an error. Interestin ...

Browsing through a collection of JSON objects with a user interface

I need a way to filter and display a list of Dogs based on a search query. I initially stored the data in an array within the state, but with an increasing number of entries, I've decided to switch to JSON format for better management. However, I&apo ...

Utilizing AJAX in Tables: Implementing an Onclick Function in a Button within a Table to Access JSON Data

I am trying to trigger a function by clicking on a button and passing the position's ID as a parameter, but I am facing some difficulties in understanding how to achieve this with the given syntax. Can anyone guide me through this process? functi ...

Using an external variable within an internal function in TypeScript

I am facing a situation where I have a variable outside of a function that needs to be updated, but I am unable to access it using "this" as it is not reachable at that point in the code. export class GamesDetailPage { details : any = {}; type : St ...

Reposition buttons using JavaScript

I attempted to modify the button that appears at the top of an HTML page. <div class='play'> <input id="play" type="button" value="Play" onclick="mode('play');startSlideshow();" /> </div> <div class='pause ...

Avoid an excessive number of XHR/AJAX requests when using the Facebook embedded iframe

I am facing an issue with a Bootstrap Carousel that contains multiple social embeds from Facebook, all of which have videos. The problem is evident on this simple jsfiddle due to the Facebook embed. If you visit this page: https://jsfiddle.net/1L95vqn4/, ...

I have encountered an issue with my rows breaking improperly when viewing them in a PDF format, particularly when I include a minus

Whenever I try to view my HTML page in PDF form, I encounter an issue. The problem arises when the numerical values are too large, causing a line break between the minus sign and the values. Here is an example of the HTML page which appears perfectly: ent ...

Enhancing socket.io with the incorporation of a variable

I was looking for a way to connect an object named player to various sockets. My initial approach was to simply do socket.prototype.player = whatever; However, no matter what I attempt to prototype, it always returns undefined. Does anyone have a solution ...

Incorporating a JSON file through a script element

A customized I18n plugin has been developed to accept various languages through json files. The goal is to simplify usage for users by allowing them to easily insert their json package directly into a page along with the script: <script id="pop-languag ...

Creating functionality with a native JavaScript plugin within a directive and test suite

I have a custom JavaScript plugin that is integrated within a directive and utilized in an Angular manner. Snippet of the directive, export default function () { 'use strict'; return { restrict: 'E', scope: { map: &apo ...

How can I implement a single-column search feature in a GridView using Javascript in ASP.NET?

I found a Google function for client-side searching in a grid using a textbox Here is the function: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> function searchFunction(phrase, ...

Using AJAX and Perl to dynamically update HTML content

Currently, I am attempting to dynamically update a div on my website using ajax and perl. The current implementation I have sort of works but needs improvement. HTML: <h2 id="cpu">--</h2> JS/ajax call: $.ajax({ type: 'GET&apos ...

StopPropagation() method in Ng-class not functioning properly in Angular for parent element

I am facing an issue with 2 nested ng-repeats. The parent one is supposed to add a class based on ng-click, but even when I click the child, it still triggers when I don't want it to. <div class="add-filter-tags" data-filter="{{f1}}" ng-class="{&a ...

What steps do I need to take to ensure that my AJAX button press request functions properly within my Django setup?

Is there a way to utilize a JavaScript string variable in a Python function and then return it back to a JavaScript variable efficiently? Perhaps using Json/ajax can help with this? To start, there is an HTML element where a string is stored: <h2 id=&q ...