创建支持 ASP.NET AJAX 的基本 Web 部件


先决条件

  Windows SharePoint Services 3.0

  Visual Studio 2005

  步骤 1:创建 Web 部件项目

  若要创建支持 AJAX 的 Web 部件控件,可以首先在 Visual Studio 2005 中的类库中创建一个类库对象。

  在 Visual Studio 2005 中创建 ASP.NET Web 部件项目

  启动 Visual Studio 2005。

  在“文件” 菜单上,指向“新建” ,再单击“项目” 。

  在“项目类型”中的“Visual Basic”或“C#”下方,选择“Windows” 。

  在“模板” 窗格中,选择“类库” 。

  键入 Sample.SayHello 作为项目名称。

  步骤 2:重命名基类并添加必需的命名空间

  创建项目之后,会显示一个空白类文件。您可以更改默认的类名称“Class1” 以轻松标识新的 Web 部件。类库项目中仅包含少量命名空间。您需要添加两个必需的命名空间以及对其程序集的引用。还必须从 System.Web.UI.WebControls.WebParts.WebPart 派生基类。然后,您必须添加两个全局变量以更新用户界面 (UI)。

  添加命名空间引用和共享的用户界面组件

  通过以下方式重命名默认类:在“解决方案资源管理器”中选择“Class1.cs” ,单击鼠标右键,再单击“重命名” ,然后键入 SayHelloWebPart 作为文件名。

  在“项目” 菜单上,单击“添加引用” 。

  在“添加引用” 对话框中的“.NET” 选项卡上,选择“System.Web.Extensions” 并单击“确定” 
对 System.Web 命名空间重复步骤 2 和 3。

  在类文件的引用区域中,添加对 System.Web.UI.WebControls 的引用并为用户界面创建两个专用变量,如下面的代码所示:

Imports System 
Imports System.Text 
Imports System.Web.UI 
Imports System.Web.UI.WebControlsImports System.Web.UI.WebControls.WebParts 
Public Class SayHelloWebPart 
  Inherits WebPart 
  Private displayName As Label 
  Private inputName as TextBox 
End Class 

  现在您已经创建了 Web 部件的基本结构。

  步骤 3:重写 CreateChildControls 并创建按钮事件处理程序

  在配置新类以用作 Web 部件之后,您必须重写 CreateChildControls 方法以构建用户界面。还必须添加按钮处理程序以刷新显示数据。

  重写 CreateChildControls 并创建按钮事件处理程序

  在 SayHelloWebPart.cs 文件中,复制并粘贴以下代码以重写 CreateChildControls 方法

Protected Overrides Sub CreateChildControls 
  MyBase.CreateChildControls 
 
  'Fix for the UpdatePanel postback behaviour. 
  EnsurePanelFix 
 
  Dim sayHello As New LinkButton 
  Dim refreshName As New UpdatePanel 
  Dim scriptHandler As New ScriptManager 
  displayName = New Label 
  inputName = New TextBox 
 
  'Set up control properties. 
  Me.displayName.ID = "displayName" 
  Me.displayName.Text = "Hello!" 
  Me.inputName.ID = "inputName" 
  sayHello.ID = "sayHello" 
  sayHello.Text = "Say Hello" 
  scriptHandler.ID = "scriptHandler" 
  refreshName.ID = "refreshName" 
  refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional 
  refreshName.ChildrenAsTriggers = True 
 
   'Add the EventHandler to the Button. 
  AddHandler sayHello.Click, _ 
   New EventHandler(AddressOf ClickHandler) 
 
  'Add the user interface (UI) controsl to the UpdatePanel 
 
  refreshName.ContentTemplateContainer.Controls.Add(Me.displayName) 
  refreshName.ContentTemplateContainer.Controls.Add(Me.inputName) 
  refreshName.ContentTemplateContainer.Controls.Add(sayHello) 
 
  'The ScriptManager must be added first. 
  Me.Controls.Add(scriptHandler) 
  Me.Controls.Add(refreshName) 
End Sub 
然后,在 SayHelloWebPart.cs 文件中,复制并粘贴以下代码:

Private Sub ClickHandler(ByVal sender As Object, _ 
       ByVal args As EventArgs) 
  Me.displayName.Text = "Hello, " & Me.inputName.Text & "!" 
End Sub 

  现在您已经创建了基本的用户界面和按钮处理事件。

  对于使用 JavaScript _doPostBack 函数提交更改的 ASP.NET 控件,可能会发生常规整页回发事件,即使 Web 部件位于 UpdatePanel 控件内也会如此。Windows SharePoint Services 3.0 和 ASP.NET AJAX 会对某些表单操作进行缓存,这会导致 SharePoint 和 ASP.NET AJAX 之间发生冲突。若要更改此行为,则必须向 Windows SharePoint Services 3.0 中运行的脚本添加代码。

  步骤 4:修改 Windows SharePoint Services 3.0 脚本以更改 doPostBack 行为

  修改脚本以确保正确的 doPostBack 行为

  在 SayHelloWebPart.cs 文件中,复制并粘贴以下代码:

Private Sub EnsurePanelFix 
  If Me.Page.Form IsNot Nothing Then 
   Dim fixupScript As New StringBuilder 
 
   With fixupScript 
    .AppendLine("_spBodyOnLoadFunctionNames.push" & _ 
    "(""_initFormActionAjax"");") 
    .AppendLine("function _initFormActionAjax") 
    .AppendLine("{") 
    .AppendLine("if (_spEscapedFormAction == " & _ 
    "document.forms[0].action)") 
    .AppendLine("{") 
    .AppendLine("document.forms[0]._initialAction = " & _ 
    document.forms[0].action;") 
    .AppendLine("}") 
    .AppendLine("}") 
    .AppendLine("var RestoreToOriginalFormActionCore = " & _ 
    RestoreToOriginalFormAction;") 
    .AppendLine("RestoreToOriginalFormAction = function") 
    .AppendLine("{") 
    .AppendLine("  if (_spOriginalFormAction != null)") 
    .AppendLine("  {") 
    .AppendLine("    RestoreToOriginalFormActionCore;") 
    .AppendLine("    document.forms[0]._initialAction = " & _ 
    "document.forms[0].action;") 
    .AppendLine("  }") 
    .AppendLine("}") 
   End With 
   ScriptManager.RegisterStartupScript(Me, _ 
    GetType(SayHelloWebPart), "UpdatePanelFixup", _ 
    fixupScript.ToString, True) 
  End If 
End Sub 


  现在您已经修改脚本以确保正确的回发处理。

  在将所有代码添加到 Web 部件对象之后,您可以构建示例 Web 部件并对其进行部署


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3