Asp .Net Button - Onclientclick="Return Function()" Vs Onclientclick="Function()"

Asp .NET Button - OnClientClick=return function() vs OnClientClick=function()

First : the client side runs (OnClientClick)

Then - the server side.

But

The client side code can prevent execution of server side by return true/false.

usually we use it for validation , before submitting to server.

Do this and your server side will ( without hacking) never work :

OnClientClick="return false;"

Executing OnClick code behind method after returning true from OnClientClick javascript function

As pointed by Rex, and as explained by several within this thread: See other thread

by changing the button to:

<asp:Button ID="btnNewsAdd" runat="server" Text="Edit current news item" OnClick="btnNewsAdd_Click" OnClientClick="if (!Confirm()) return false;"/>

and the Javascript to:

<script type="text/javascript">
function Confirm() {
return confirm("Are you sure?");
}
</script>

problem is solved.

OnClientClick not passing to OnClick code behind on return of true in my ASP.NET web solution

Try this...you really don't even need that JS function.

<asp:Button ID="btnAdd" runat="server" OnClientClick="return window.confirm('Are you sure?');" Text="Add" OnClick="btnAdd_Clicked"/>

protected void btnAdd_Clicked(object sender, EventArgs eventArgs)
{
string test = "12345";
}

If you debug the btnAdd_Clicked function, you'll see it hits the first line of code there.

calling javascript function on OnClientClick event of a Submit button

OnClientClick="SomeMethod()" event of that BUTTON, it return by default "true" so after that function it do postback

for solution use

//use this code in BUTTON  ==>   OnClientClick="return SomeMethod();"

//and your function like this
<script type="text/javascript">
function SomeMethod(){
// put your code here
return false;
}
</script>

ASP.NET OnClientClick=return false; doesn't work

Do you have a click event handler (registered via jquery) which returns true? In that case, the return value of OnClientClick is ignored.

Have a look at my question (and answer): Why doesn't returning false from OnClientClick cancel the postback

Simple JavaScript problem: onClick confirm not preventing default action

There's a typo in your code (the tag a is closed too early).
You can either use:

<a href="whatever" onclick="return confirm('are you sure?')"><img ...></a>

note the return (confirm): the value returned by scripts in intrinsic evens decides whether the default browser action is run or not; in case you need to run a big piece of code you can of course call another function:

<script type="text/javascript">
function confirm_delete() {
return confirm('are you sure?');
}
</script>
...
<a href="whatever" onclick="return confirm_delete()"><img ...></a>

(note that delete is a keyword)

For completeness: modern browsers also support DOM events, allowing you to register more than one handler for the same event on each object, access the details of the event, stop the propagation and much more; see DOM Events.

Disable button on form submission

Give this a whirl:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

// Identify button as a "disabled-when-clicked" button...
WebHelpers.DisableButtonOnClick( buttonTest, "showPleaseWait" );
}

protected void buttonTest_Click( object sender, EventArgs e )
{
// Emulate a server-side process to demo the disabled button during
// postback.
Thread.Sleep( 5000 );
}
}

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Text;

public class WebHelpers
{
//
// Disable button with no secondary JavaScript function call.
//
public static void DisableButtonOnClick( Button ButtonControl )
{
DisableButtonOnClick( ButtonControl, string.Empty );
}

//
// Disable button with a JavaScript function call.
//
public static void DisableButtonOnClick( Button ButtonControl, string ClientFunction )
{
StringBuilder sb = new StringBuilder( 128 );

// If the page has ASP.NET validators on it, this code ensures the
// page validates before continuing.
sb.Append( "if ( typeof( Page_ClientValidate ) == 'function' ) { " );
sb.Append( "if ( ! Page_ClientValidate() ) { return false; } } " );

// Disable this button.
sb.Append( "this.disabled = true;" );

// If a secondary JavaScript function has been provided, and if it can be found,
// call it. Note the name of the JavaScript function to call should be passed without
// parens.
if ( ! String.IsNullOrEmpty( ClientFunction ) )
{
sb.AppendFormat( "if ( typeof( {0} ) == 'function' ) {{ {0}() }};", ClientFunction );
}

// GetPostBackEventReference() obtains a reference to a client-side script function
// that causes the server to post back to the page (ie this causes the server-side part
// of the "click" to be performed).
sb.Append( ButtonControl.Page.ClientScript.GetPostBackEventReference( ButtonControl ) + ";" );

// Add the JavaScript created a code to be executed when the button is clicked.
ButtonControl.Attributes.Add( "onclick", sb.ToString() );
}
}


Related Topics



Leave a reply



Submit