2

I am using this code block from a previous SO post showing ClientScript Alerts before Redirecting to another page in ASP.NET c#? that will display an alert message in an aspx page before redirecting to another page. I modified this script to avoid the redirect part.

However, this page does a submit. How can I avoid doing a page submit?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

    public class HTMLHelper
    {
        public static void jsAlertAndRedirect(System.Web.UI.Page instance, string Message,string url)
        {
            instance.Response.Write(@"<script language='javascript'>alert('" + Message + "'); </script>");
        }
    }



HTMLHelper.jsAlertAndRedirect(this, "This is an alert.");
Community
  • 1
  • 1
LearningCSharp
  • 1,292
  • 2
  • 13
  • 26

2 Answers2

3

You can add return false; to your code

<script language='javascript'>instructions ....; return false; </script>
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
0

if you want to call it on any button then like below you can do it: Javascript code

$('[id$=btntest]').live('click',function(e){
    e.preventdefault();
    });

or you can do like below also

 $('[id$=btntest]').live('click',function(e){
    return false;
    });
Ram Singh
  • 6,664
  • 35
  • 100
  • 166