What is the Ext.NET equivalent of an ASP.NET custom validator? I need to call a server side method for validation. Can I get an example of doing this?
Printable View
What is the Ext.NET equivalent of an ASP.NET custom validator? I need to call a server side method for validation. Can I get an example of doing this?
Hello!
There is no own custom server validation mechanism in the Ext.Net. But you could use <asp:CustomValidator>.
Example
Also don't forget there is very flexible mechanism of client side validation in the Ext.Net. If your scenario allows to store data for validation within a page (as Hidden fields) you could use that. If it needs to do some actions on a server you could use DirectMethods from JavaScript validation function.Code:<%@ Page Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
void Button_Click(object sender, EventArgs e)
{
// Display whether the page passed validation.
if (Page.IsValid)
{
Label1.Text = "Page is valid.";
}
else
{
Label1.Text = "Page is not valid!";
}
}
void ServerValidation(object source, ServerValidateEventArgs args)
{
try
{
// Test whether the value entered into the text box is even.
int i = int.Parse(args.Value);
args.IsValid = ((i % 2) == 0);
}
catch (Exception ex)
{
args.IsValid = false;
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ext.NET Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
<ext:Label ID="Label1" runat="server" Text="Enter an even number:" />
<br/>
<ext:TextField ID="TextField1" runat="server" />
<asp:CustomValidator
runat="server"
ControlToValidate="TextField1"
OnServerValidate="ServerValidation"
ErrorMessage="Not an even number!" />
<ext:Button runat="server" Text="Validate" AutoPostBack="true">
<DirectEvents>
<Click OnEvent="Button_Click" />
</DirectEvents>
</ext:Button>
<br/>
</form>
</body>
</html>
Hello again!
Also you could use RemoteValidation. Please look at the example.
Example
Code:<%@ Page Language="C#" %>
<%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Remote Validation - Ext.NET Examples</title>
<style type="text/css">
.validation-indicator{
background-image: url(/extjs/resources/images/default/grid/loading-gif/ext.axd) !important;
}
</style>
<script runat="server">
protected void CheckField(object sender, RemoteValidationEventArgs e)
{
TextField field = (TextField)sender;
if (field.Text == "Valid")
{
e.Success = true;
}
else
{
e.Success = false;
e.ErrorMessage = "'Valid' is valid value only";
}
System.Threading.Thread.Sleep(3000);
}
</script>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
<ext:FormPanel runat="server"
Title="Remote Validation Form"
Padding="5"
Frame="true"
LabelWidth="250"
MonitorValid="true"
Width="500"
DefaultAnchor="-20"
Height="200">
<Items>
<ext:TextField runat="server" FieldLabel="No validation" />
<ext:TextField runat="server" FieldLabel="Client only validation" AllowBlank="false" />
<ext:TextField runat="server" FieldLabel="Server only validation" IsRemoteValidation="true">
<RemoteValidation
OnValidation="CheckField"
Before="this.setIndicatorIconCls('validation-indicator');this.setIndicatorTip('Validating...');"
Complete="this.clearIndicator()" />
</ext:TextField>
<ext:TextField
runat="server"
FieldLabel="Client and server validation"
IsRemoteValidation="true"
AllowBlank="false">
<RemoteValidation
OnValidation="CheckField"
Before="this.setIndicatorIconCls('validation-indicator');this.setIndicatorTip('Validating...');"
Complete="this.clearIndicator()" />
</ext:TextField>
</Items>
<Buttons>
<ext:Button runat="server" Text="Submit" FormBind="true" />
</Buttons>
</ext:FormPanel>
</form>
</body>
</html>