Monday, March 26, 2012

causing validation from client side without postback?

Now that I added callout extenders to my validators how can I get them to work 1- on the client before calling an AJAX webservice and 2- in the webservice? I am not using an update panel so can I turn off server side validation and write my own for the webservice to call?

Hi Echo,

Echo88:

Now that I added callout extenders to my validators how can I get them to work 1- on the client before calling an AJAX webservice and 2- in the webservice? I am not using an update panel so can I turn off server side validation and write my own for the webservice to call?

My understanding of your issue is that you want to validate the inputs. If all these are correct then call the WebService ,otherwise, show the error message by using ValidatorCalloutExtender. If I have misunderstood, please feel free to let me know.

Validators are call before the form is submitted , but to call the WebService we don't need to postback. So I suggest that you should write your own validate Javascript code instead of ValidatorCalloutExtender controls.

Here is the sample to call a WebService.

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="DataTableService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager
<input type="button" value="Get DataTable" onclick="getDataTable();" /
<div id="result"></div
<script language="javascript" type="text/javascript">
function getDataTable()
{
DataTableService.GetDataTable(onSucceeded, onFailed);
}

function onSucceeded(result)
{
//handle code
}

function onFailed(error)
{
//handle code
}

You can also transfer all the inputs and validate on the server side in the WebService.

I hope this help.

Best regards,

Jonathan


Thanks - sucks that I can not use those jazzy callouts. But a question I have is - doesn't it call a client script prior to postback?? My understanding is that validation takes place before posting the form data to the server, so one would think there is a call to a script that precedes the actual postback. Am I wrong?


Hi Echo88,

But a question I have is - doesn't it call a client script prior to postback??

Yes , you are right. The problem is that if all the inputs are correct , the form will postback next. But to call a webservice , we usually do not need to postback. It will cause the page refreshment. Based on this , I think write your own Javascript function is a better way in this situation. If you don't want to validate on the client side , you can also transfer all the inputs to the server side and validate on the WebService.

function setBoy()
{
var boy = new Object();
boy.Name = "Terry";
var girl = new Object();
girl.Name = "Mary";
boy.GirlFriend = girl;

BoyGirlService.SetBoyWithGirlFriend(boy, onSetBoySucceeded, onFailed); // call the WebService.
}

I hope this help.

Best regards,

Jonathan


exactly - I am calling a webservice instead of a postback. But am I right to say there should be a way to cause validation on the client - regardless of postback? I just thought there would be a method to execute independently. What aboutPage_ClientValidate() ??


Hi Echo88,

Yes , I agree with you. You can use it like this.

Button1.Attributes["OnClick"] = "Javascript:if (typeof(Page_ClientValidate) == 'function') if(Page_ClientValidate()) {return confirm('Are you sure to submit ?'); } else return false;";

Best regards,

Jonathan


I tried that - no dice. The button is set to cause validation and uses submit behavior. However, nothing is happening? I guess it is not finding Page_ClientValidate function. When include Page_ClientValidate by itself - it throws an error. So why is not included?


Hi Echo88,

Sorry for the delay. Here is my test sample and it works pretty well.

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e) { Button1.Attributes["OnClick"] = "Javascript:if (typeof(Page_ClientValidate) == 'function') if(Page_ClientValidate()) {return confirm('Are you sure to submit ?'); } else return false;"; }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"><%=DateTime.Now.ToString()%> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator> <asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="true" CausesValidation="true" /> </form></body></html>
So please compare it with yours and if it doesn't work, please feel free to let me know with more information. Thanks.

Best regards,

Jonathan


Your example works - even though it does perform a postback - I turned off the submitbehavior. Still works:) so there is hope- I suspect the

Page_ClientValidate function is not getting generated somehow. Could the fact that I am using them all as part of a user control be the problem?
Thanks - its really appreciated. 

Ok - I found out something interesting... The Page_Validators array is not populating on this one page. Any ideas why they are not populating Page_Validators ?


OK - I found the root of the cause. In my control I have readonly mode that would make all validators invisible if it was in readonly mode. Apparently, I did set the function incorrectly and all validators were not being rendered.

No comments:

Post a Comment