I'm currently working on calling a Page Method using AJAX. Take a look at the code below:
<asp:Button ID="btn_upload" runat="server" CssClass="btnEffects" Text="Upload"
onclick="btn_upload_Click" OnClientClick="return Validate();" />
The Validate method is expected to return true or false. Here's the code for the Validate method:
<script language="javascript">
function Validate()
{
var filename = $get('<%= txt_filename.ClientID %>').value;
PageMethods.IsValidFile(filename,OnSuccess, OnFailure);
// IsValidFile is a Page Method of bool return type
}
function OnSuccess(result)
{
if ( !result)
{
alert('File '+ $get('<%= txt_filename.ClientID %>').value + ' does not exist');
return false;
}
else
{
return true;
}
}
function OnFailure(error)
{
}
</script>
My issue is that even after displaying the alert 'File somefilename does not exist', the entire page refreshes (a postback occurs).