Search

Nov 28, 2007

How to know page validity from java script

Hello All,

I was having one requirement which is as follows.

- Once you click on submit button, the button must be disabled
- All the validator should work properly then and only then button get disabled [obvious thing]
- And the page needs to be submitted back to server as well.

I was looking for the method where I can find the page valid property or value which allow me to do what I want.

There is a property Page_IsValid in java script which let me know the validity of page. But it will always set to false first time.

So I found the solution which help me to fulfill my requirement.

Have a look at following code

<script language="javascript" type="text/javascript">

function btnSaveClientClick(objBtn)
{
var isPageValid = Page_ClientValidate();
if(isPageValid)
{
objBtn.disabled = true;
__doPostBack(objBtn.name,'');
}
}

</script>

This is the server side control [submit button]

<asp:Button ID="btnSave" runat="server" SkinID="button_plain" OnClientClick="javascript:return btnSaveClientClick(this);" Text="Save" OnClick="btnSave_Click" />

How it works:

- On client click of save button btnSaveClientClick() method get executed with single parameter this which is button itself.
- Page_ClientValidate() method used for checking client site validation, will return true or false.
- objBtn.disabled, will disable the button because objBtn is reference to our Save Button
- And last need to postback to server, so call __doPostBack(objBtn.name,'');

That's it!