w0051977
w0051977

Reputation: 15817

OnClientClick causes webpage refresh

I have an ASP.NET button on a webpage:

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

and a JavaScript function:

 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;
    }  

When I click on the button; the JavaScript function runs and then the webpage refreshes. Why does the webpage refresh? According to this link; returning FALSE should stop the webpage from refreshing: Stop page reload of an ASP.NET button

Upvotes: 1

Views: 2387

Answers (1)

SanketS
SanketS

Reputation: 973

Use return for clientclick.

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

OR you can simply use html button of server side coding is not required then.

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

Upvotes: 5

Related Questions