Calling code/script from a Rule

本文介绍如何在Microsoft InfoPath中通过规则调用自定义代码的方法,包括使用VBScript、JScript、VB.NET及C#编写的函数,并展示了如何利用xdExtension命名空间在规则条件下调用这些函数。

转:http://blogs.msdn.com/infopath/archive/2005/06/17/430347.aspx

Rules are a quick way to add dynamic functionality to a form, so a lot of programmers choose to use rules even when they know how to write the code to do the same thing. However, sometimes after writing a complex set of rules they may realize that they need to do something rules doesn’t support, and we know they don’t want to rewrite all their rules in code just to add that one little bit.

Alas, there’s no rules action type for “Call code”, but there is a roundabout way to call your code anyway.

Let’s say you have the following function in your form template:

VBScript
Function Test(param)
  XDocument.UI.Alert("Param: " & param)
  Test = true
End Function
Jscript
function Test(param) {
  XDocument.UI.Alert("Param: " + param);
  return true;
}
VB.Net
Public Function Test(ByVal param As String) As Boolean
  thisXDocument.UI.Alert("Param: " & param)
  Test = True
End Function
C#
public boolean Test(string param) {
  thisXDocument.UI.Alert("Param: " + param);
  return true;
}

You can call that function from the condition of a rule by using the xdExtension namespace.

For example, here’s how to call the function from a rule when the form is opened:

  1. Click Form Options on the Tools menu.
  2. On the Open and Save tab, click Rules.
  3. Click Add, name the rule, and then click Set Condition.
  4. Select The expression in the first drop-down list, and then enter: xdExtension:Test(“foo”)
  5. If you want the function to be conditional, then put the condition before the function call with an AND clause. For example, to get if foo>bar, then Test(“foo”), use foo>bar and xdExtension:Test(“foo”). (This is possible because conditions are evaluated using short-circuit boolean evaluation. In other words, they are evaluated one clause at a time, and if the first clause determines the result, then the rest of the conditions are “short circuited” and don’t get executed.)
  6. The Rule dialog box requires at least one action, but if you don’t have any actions to run in that rule, you can click OK if you select the Stop processing rules when this rule finishes checkbox. To avoid actually stop processing rules, make your condition always return false. For example: xdExtension:Test(“foo”) and false (“false” here is actually looking for the node named “false” which doesn’t exist, so returns the empty set, which is translated to false(). You could just as easily use “foobar” or anything else.)

Finally, extract your form files and add the following attribute to the manifest.xsf file’s xDocumentClass element:

xmlns:xdExtension=http://schemas.microsoft.com/office/infopath/2003/xslt/extension

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值