Clicking on a client causes the webpage to refresh

One particular webpage contains an ASP.NET button with the following code:

<asp:Button ID="TickButton" runat="server" OnClientClick="SelectSome()"  Text="Tick" />

This button is linked to a JavaScript function like this:

 function SelectSome() {
        var id = document.getElementById("ctl00_ContentPlaceHolder1_txtSelectSome").value;
        if (isNaN(id)==false)
        {
            var frm = document.forms[0], j = 0;
            for (i = 0; i < frm.elements.length; i++) {
                if (frm.elements[i].type == "checkbox" && j < id) {
                    frm.elements[i].checked = true;
                    j++;
                }
            }
        }
        else
        {
            alert("You must enter a number.")
        }
        return false;
    }  

Surprisingly, when the button is clicked, the JavaScript function executes but then triggers a refresh of the webpage. This behavior goes against expectations as returning FALSE from the function should prevent the refresh as indicated in this useful link: Stop page reload of an ASP.NET button

Answer №1

Make sure to utilize the return statement for clientclick events.

<asp:Button ID="TickButton" runat="server" OnClientClick="return SelectSome()"  Text="Tick" />

Alternatively, if server-side coding is not necessary, you can opt for a simple HTML button instead.

<asp:Button Text="Tick" runat="server" OnClientClick="return SelectSome()" />

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

Combining formData props with Component state in React using Redux

Monitoring form input changes and saving them to the state in my Editor Component. Upon form submission, dispatching the formData from the state to the store. Upon Component reloads, intending to merge the prop's formData with the state. Attempted u ...

Tips for sending a Django queryset as an AJAX HttpResponse

Currently, I am faced with the challenge of fetching a Django queryset and storing it in a JavaScript variable using Ajax. I have attempted to employ the following code snippet for this purpose; however, I keep encountering the issue of "Queryset is not J ...

Is It Possible to Create Flash Content Without Using a SWF File?

Is there a way to embed Flash directly in HTML, rather than linking to an external SWF file? I am looking to send an HTML form via email for the recipient to fill out by opening it in a browser. The final step would involve copying the result to their clip ...

JavaScript unable to access cookie on the initial attempt

I have been utilizing JavaScript to set and retrieve cookie values. The code I am using can be found at http://www.w3schools.com/js/js_cookies.asp. Upon page load, I check if the cookie exists or not. Everything is functioning properly except for the issue ...

What is the best way to pass a variable between all methods in the code-behind file

Currently, I am faced with the task of fixing a less than stellar ASP.net 4.0 application that was passed on to me. The original developers did not adhere to the usual asp.net standards, which has posed some challenges for me. While I am still relatively n ...

show JSON data following an Ajax request (Asynchronous)

I'm encountering an "undefined" error while attempting to render raw JSON using JSON.parse (result). function decodeVin(result) { var vinArray = JSON.parse(result); var results = "<h4>Vehicle Information Result:</h4>"; results += "Year: ...

Using JQuery to assign text to labels within web controls is a simple and effective way to enhance user interaction and improve

I have a situation where I need to use jQuery to assign the text "hello" to the label "lbl". When accessing the label "lbl", it should now display the text "hello" instead of the original value "hai". In my aspx.cs file, I have added the following code: ...

JavaScript: The hyperlink provided in the data-href attribute is not functioning properly within the carousel

I have a carousel with images that I want to link to specific pages: JS Fiddle. My goal is to have each image in the carousel direct users to a different webpage when clicked. For example: Clicking on the wagon image should go to wagon.com Clicking on th ...

Error: implode() function received invalid arguments

I have a form that sends data from one page to another using jQuery. Here is an example of how it works: <?php $counter = 0; $sqlmasmohType = mysql_query("select * from masmoh"); while ($rowmasmohType = mysql_fetch_array($sqlmasmohType ...

Puppeteer refuses to interact with Facebook's cookie banner, no matter the circumstances

I'm currently working on a script that automates the login process for Facebook by loading the website, entering an email and password, and logging in automatically. However, I've run into an issue where a cookie notice pops up after about 0.5 se ...

Main navigation category as parent and secondary category as a child menu in WordPress

Hello there, I am looking to create a navigation menu with parent categories displayed horizontally and child categories as corresponding submenus. For example, The parent categories would be Apple, Orange, and Mango Under Apple, we would have apple1, ...

Unable to display Angular Directive

Despite my efforts to understand directives, I keep encountering the same issue - I can't seem to get one to work properly in order to import content from another HTML file. Here is the structure of my project: <!DOCTYPE html> <html ng-app= ...

Managing xhr requests using Node.js

I recently delved into the world of nodejs and I must say, I'm quite impressed with it. However, I've hit a snag when attempting to utilize it as a middle-man server/client. Just to clarify, my goal is to use nodejs on the client side to act as ...

Creating a WebSocket service similar to stackoverflow using Node.js: A step-by-step guide

I'm currently developing a chat application using node.js. I have implemented a feature to receive new messages via ajax, where requests are sent every 3 seconds. However, I recently observed that stackoverflow handles fetching new data differently. I ...

Copying to the clipboard now includes the parent of the targeted element

Edit - More Information: Here is a simplified version of the sandbox: https://codesandbox.io/s/stupefied-leftpad-k6eek Check out the demo here: https://i.sstatic.net/2XHu1.jpg The issue does not seem to occur in Firefox, but it does in Chrome and other ...

Comparing the length of an array to whether the length of the array is greater than

What distinguishes checking an array's length as a truthy value from verifying that it is greater than zero? In simple terms, is there any advantage in utilizing one of these conditions over the other: var arr = [1,2,3]; if (arr.length) { } if (arr ...

Pressing the button will navigate to page X initially, and then after the countdown finishes, it will

A unique surprise is in store for my wife with this special website. The home page features a countdown to a significant date, and only one button unlocks the gift. I am looking for a way to either keep the button locked until the countdown ends or have it ...

Styling links conditionally in a React component

How can I style the page title in my nav bar when I am on the selected path? I am using React and Tailwind CSS. For example, I want the title to turn yellow when I am on the current page. Here is the logic I have tried, but it doesn't seem to be work ...

Safety measures for thread when utilizing a static property as an instance of the class

Please review the following code snippet: public class BusinessClass { static BusinessClass myInstance { get; set; } Repository repository; public BusinessClass() { if (repository == null) repository = new RepositoryCl ...

Encountering a NoSuchElementException when transitioning from frame[0] to window[1] in Firefox GeckoDriver using Selenium with Python

Encountered an issue with the Firefox GeckoDriver browser where I receive an error stating `element not found`. The problem arises when I navigate from window[1] to frame[0], back to window[1], and then attempt to click the close frame button. I prefer u ...