arrest warrant
arrest warrant

Reputation: 354

Why Aspx page get reloaded after javascript call

Below is the code of a simple page with a button.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
CodeBehind="AdminPage.aspx.cs" Inherits="School.AdminPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
    function calling() {
        var user = "all";
        alert(user)
    }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<button runat="server" onclick="calling()">
    Mybutton</button>
</asp:Content>

On click of button, alert appear and when i click ok on that alert dialog box...page gets reloaded....why it get reloaded and how to stop this.

Upvotes: 2

Views: 840

Answers (2)

Hitesh S
Hitesh S

Reputation: 580

Use this code to close alert without refreshing page

<script type="text/javascript">
    window.onbeforeunload = function() {
        return ;
    }
</script>

Upvotes: 0

शेखर
शेखर

Reputation: 17614

The "default event" is occurring.

To disable it, use return before you function call as follows:

<button runat="server" onclick="return calling()">

And in the function:

<script type="text/javascript">
   function calling() {
       var user = "all";
       alert(user);
       return false;
   }
</script>

Upvotes: 4

Related Questions