Sometimes it is desirable to check validity of the given filed on the server side.
This is possible with the use of the ServiceConstraint control.
The ServiceConstraint sends a web request to the server, with the defined
parameters, and returns either true or false.
To specify the address of the webservice, use the ServicePath property.
To specify the name of the webmethod, use the ServiceMethod property.
The parameters that are sent to the webservice are defined as inner content properties
with the use of the ServiceParameter control.
The ServiceParameter control should be defined with the use of the
following properties:
Name - the name of the variable, used in the webmethod
Path - the xpath-like path to the MForm item, whose value will be sent
The server-side webmethod must contain the [ScriptMethod] attribute,
and the class that this webmethod is within must contain the [ScriptService]
attribute.
The definition of this method must match the definition of the ServiceConstraint
control:
- The name of the method must match the value of the
ServiceMethod property
- The order of the
ServiceParameter properties must match the order of
method variables
- The name of the
ServiceParameter property must match the name of the
corresponding method variable
- The
ServiceParameter value must be serializable with the use of its
corresponding method's type
- Example:
-
If the definition of the
ServiceConstraint looks like this:
<mfadd:ServiceConstraint runat="server" ServicePath="some/service/path" ServiceMethod="MethodName" >
<mfadd:ServiceParameter runat="server" Name="someParameter" Path="some/parameter/path" />
<mfadd:ServiceParameter runat="server" Name="otherParameter" Path="other/parameter/path" />
</mfadd:ServiceConstraint>
then the method's definition should look like this:
[ScriptMethod]
public ServiceConstraintResponse MethodName(someType someParameter, otherType otherParameter) {
...
}
The ServiceConstraint also provides a functionality to invoke a client-side method instead of a server-side service.
This can be done by specifying the CustomScript property.
When the CustomScript property is set, it takes precedence over the ServicePath and ServiceMethod settings, which in such a situation are not used.
The CustomScript should be a method, that takes two parameters:
- sender - the client-side ServiceConstraint object;
- args- the event args, which contain
parameters property with a hashtable of all the parameters;
The method should return an object with two properties set:
- IsValid - an information whether the validation was successful
- ErrorText - the error text that should be displayed