How to execute JScript code from an InfoPath 2007 browser-enabled form template(转)

转自:http://ossmall.ro/how-to-execute-jscript-code-from-an-infopath-2007-browser-enabled-form-template/

此问题已困扰了很久,很希望通过这篇文章介绍的方法来解决Infoapth表单中以TreeView方式选择Organization

 

Learn how to execute JScript code from InfoPath browser-enabled form templates that are hosted in custom ASP.NET pages.

Abstract

This article explains how to execute JScript code from InfoPath browser-enabled form templates that are hosted in custom ASP.NET pages. It assumes that you are familiar with designing and publishing InfoPath form templates, InfoPath Forms Services, the XmlFormView control, ASP.NET, JScript, and writing .NET code for InfoPath form templates.

Introduction

While you cannot publish a form template that contains script to a server running InfoPath Forms Services, you can use the XmlFormView control to host a browser-enabled form template in a custom ASP.NET page and indirectly execute JScript code from the form template. To do this, you must:

1. In an event handler of the form template, use the NotifyHost method [http://msdn2.microsoft.com/en-us/library/microsoft.office.infopath.xmlform.notifyhost(VS.80).aspx] of the XmlForm class.
2. In the ASP.NET page, hook up the NotifyHost event [http://msdn2.microsoft.com/en-us/library/microsoft.office.infopath.server.controls.xmlformview.notifyhost.aspx] of the XmlFormView control to an event handler.
3. Register and execute JScript code from within the event handler in the ASP.NET page.

For more information about the topics discussed in this article, see Hosting the InfoPath 2007 Form Editing Environment in a Custom Web Form [http://msdn2.microsoft.com/en-us/library/aa701078.aspx].

Step 1: Using the NotifyHost Method to Send Notifications

You can use the NotifyHost method of the XmlForm class to send notifications to the hosting environment of a form template. Perform the following steps to add a Button control and its event handler to a form template, and call the NotifyHost method:

1. Open the InfoPath browser-compatible form template in design mode.
2. In the task pane, click Controls.
3. Add a Button control to the form template.
4. Right-click the Button control and select Button Properties.
5. On the Button Properties dialog box, click Edit Form Code to add a Clicked event handler to the form template’s business logic code.
6. Add the following code to the Button control’s Clicked event handler:

C#

NotifyHost("Message from InfoPath");

Visual Basic

NotifyHost("Message from InfoPath")

In the previous example, the form will send the notification, “Message from InfoPath”, to the custom ASP.NET page when you click the button. While the notification must be a string, it need not be static. For example, you can retrieve the value of a field on the form and send this value as a notification. You can also pass an empty string to the NotifyHost method just to alert the ASP.NET page that an event occurred in the form.

Note: Since the form template contains managed code, you will have to perform an administrator-approved deployment to publish the form template to a server running InfoPath Forms Services.

Step 2: Hooking Up the NotifyHost Event to an Event Handler

The NotifyHost method raises the NotifyHost event of the XmlFormView control. You must hook up and implement an event handler for the NotifyHost event before the ASP.NET page can receive notifications from the form.

In the following example an XmlFormView control named XmlFormView1 has been added to a custom ASP.NET page. The control hosts a browser-enabled form template named MyFormTemplate.xsn, which was published by an administrator to the form template library, FormServerTemplates, of a site collection. ServerName is the name of the server that is running InfoPath Forms Services.

To hook up the NotifyHost event of XmlFormView1 to an event handler named XmlFormView1_NotifyHost, you must add OnNotifyHost=”XmlFormView1_NotifyHost” to the control tag of XmlFormView1. The following code shows how to do this:

HTML

<cc1:XmlFormView ID="XmlFormView1" runat="server"               

    XsnLocation="http://ServerName/FormServerTemplates/MyFormTemplate.xsn" 

    OnNotifyHost="XmlFormView1_NotifyHost"

/>

After you hook up the event, you must implement the XmlFormView1_NotifyHost event handler in the code-behind of the ASP.NET page as follows:

C#

protected void XmlFormView1_NotifyHost(object sender, NotifyHostEventArgs e)

{

    ...

}

Visual Basic

Protected Sub XmlFormView1_NotifyHost(ByVal sender As Object, _ 

                                      ByVal e As NotifyHostEventArgs)

    ...

End Sub

The NotifyHostEventArgs argument of the event handler has a property named Notification. This property contains the notification that the InfoPath form sends when you use the NotifyHost method. The following code retrieves and stores the notification in a string variable:

C#

string message = e.Notification;

Visual Basic

Dim message As String = e.Notification

Note: To use the NotifyHostEventArgs class, you must import the Microsoft.Office.InfoPath.Server.Controls namespace.

Step 3: Executing JScript Code from the NotifyHost Event Handler

Once you have hooked up the NotifyHost event to an event handler, you can write code in the event handler to register and execute JScript code.

Example 1: Displaying a message box The following code displays a message box with the notification message that was sent from the InfoPath form.

C#

protected void XmlFormView1_NotifyHost(object sender, NotifyHostEventArgs e)

{

    string jsCode = String.Format("alert('{0}');", e.Notification);

    ClientScript.RegisterStartupScript(typeof(string), "AlertScript",                   

                                       jsCode, true);

}

Visual Basic

Protected Sub XmlFormView1_NotifyHost(ByVal sender As Object, _

                                      ByVal e As NotifyHostEventArgs)

    Dim jsCode As String = String.Format("alert('{0}');", e.Notification)

    ClientScript.RegisterStartupScript(GetType(String), "AlertScript", _

                                       jsCode, True)

End Sub

Example 2: Opening a new browser window The following code opens a new browser window and navigates to a specified URL. Here, the NotifyHost method is only used to alert the ASP.NET page that an event occurred in the InfoPath form, so the notification message that was sent is ignored.

C#

protected void XmlFormView1_NotifyHost(object sender, NotifyHostEventArgs e)

{

    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    sb.Append("window.open(");

    sb.Append("'http://support.microsoft.com/default.aspx', ");

    sb.Append("'mywindow', 'width=640,height=480'");

    sb.Append(");");

    ClientScript.RegisterStartupScript(typeof(string), "NewWinScript",                   

                                       sb.ToString(), true);

}

Visual Basic

Protected Sub XmlFormView1_NotifyHost(ByVal sender As Object, _

                                      ByVal e As NotifyHostEventArgs)

    Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()

    sb.Append("window.open(")

    sb.Append("'http://support.microsoft.com/default.aspx', ")

    sb.Append("'mywindow', 'width=640,height=480'")

    sb.Append(");")

    ClientScript.RegisterStartupScript(GetType(String), "NewWinScript", _

                                       sb.ToString(), True)

End Sub

Conclusion

You cannot publish a form template that contains script to a server running InfoPath Forms Services. However, you can host a browser-enabled form template in an XmlFormView control on a custom ASP.NET page and use the NotifyHost method of the XmlForm class to send notifications from the InfoPath form to its host. This raises a NotifyHost event in the XmlFormView control, which you can then catch and handle in an event handler of the ASP.NET page. Finally, you can register and execute JScript code from within the event handler.

——————————————–

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material. Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

 

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

原文地址:https://www.cnblogs.com/sdikerdong/p/3225595.html