views:

1777

answers:

4

I have an asp.net server control (with the asp: in its definition). The button has been set to do post back.

On the server side, I have the on click event handler e.g btnSave_click()

On the client side, I have a javascript function to be invoked on the click event e.g btnSave.Attributes.Add("onclick","javascript: return CheckIsDirty();")

Am not sure which order these two will be executed. Because I want first on the client side to warn of any data entry fields that are not yet filled-out before actually saving any data.

Any help?

+2  A: 

First client side, second server-side.

So you can use it.

I also use it in some cases, like:

close.Attributes["OnClick"] = "return confirm('Are you sure?')";

In this case if the user presses 'No' then the server-side event handler does not even play a role.

Biri
Thanks, that works for me now...
J Angwenyi
A: 

I think you need a much better understanding of what it means client side and what it means server side and how they all relate together. I've seen more and more developers make a mess of it.

Of course the client side will execute first in your case. Actually there's no way to execute it after the server code is executed (except if you do something manually). I'll try to give a brief explanation:

Whatever you have in your server, will generate some HTML on the client and the user is always interacting on the client. So you have a html button that the user is clicking. What the browser will do is execute the javascript associated with it or if no javascript is specified and the button is a submit button it will submit the form. if you check the generated html you will see that for the onclick event you will have the script you have added followed by some autogenerated script that actually will submit the form to the server. Your server side code will execute only if the page will be submitted.

Albert
+2  A: 

Hi

The trick here is to set this global variable "Page_IsValid" false if your test fails and this will stop the post back.

Read this page http://msdn.microsoft.com/en-us/library/aa479045.aspx which explains both server side and client Validation. There is sone good code example you can use.

Pbearne
+1  A: 

The way you are setting your onClick JavaScript event will actually prevent it from posting back as you are overwritten the ASP.NET event handler. The correct way to accomplish the validation you are intending is to:

btnSave.Attributes.Add("onclick", "CheckIsDirty();" + GetPostBackEventReference(btnSave).ToString());

Notice that you append the result of GetPostBackEventReference, so that in JavaScript you first call your CheckIsDirty() method and then call the ASP.NET postback method. Assuming your method returns true, then the button will post. If it returns false then it will not cause a post back.

Does that sound like what you are trying to accomplish?

Jason Whitehorn