65.9K
CodeProject 正在变化。 阅读更多。
Home

AjaxConnectedPageViewer

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.75/5 (4投票s)

2008年3月17日

CPOL
viewsIcon

28068

downloadIcon

183

借助 Microsoft ASP.NET AJAX 1.0,您可以构建更具动态性的应用程序,使其更接近于流畅、无中断的交互体验。此 Web 部件将全面了解站点集合作为树状视图,以及相互连接的各自属性作为数据网格。

引言

这是我作为 AJAX 可连接 Web 部件开发周期中的第二个版本。在进行一些研发时,我发现 ICellConsumerICellProvider 接口在 AJAX 框架中编写可连接 Web 部件时更有效。

背景

这是我的第二篇文章,为了更深入地了解 AJAXConnectableWebPart,请查看我之前的文章:AjaxConnectableWebPart_V1.0.0.0

使用代码

首先,我们需要从 ICellProvider 接口派生我们的 Web 部件

/// <summary>
/// This is an ajax-enabled webpart that sends
/// URL to a connectable PageViewer webpart
/// </summary>
public class AjaxUrlListWP : WebPart, ICellProvider
{
}

ICellProvider 需要一些事件

//Events required by ICellProvider
public event CellProviderInitEventHandler CellProviderInit;
public event CellReadyEventHandler CellReady;
/// <summary>
/// This method is called by the Web Part infrastructure to notify the Web Part 
/// that it has been connected.
/// </summary>
/// <param name="interfaceName">Friendly name of the interface
///              that is being connected.</param>
/// <param name="connectedPart">Reference to the other Web Part
///              that is being connected to</param>
/// <param name="connectedInterfaceName">Friendly name of the interface
/// on the other Web Part through which they are connected</param>
/// <param name="runAt">Where the interface can be executed</param>
public override void PartCommunicationConnect(string interfaceName,
    WebPart connectedPart, string connectedInterfaceName, ConnectionRunAt runAt)
{
    //Receive connection from "Cell_Provider_Interface_WPQ_" interface only.
    if (interfaceName == "Cell_Provider_Interface_WPQ_")
    {
        //the connection is accepted, and the web part is connected now
        isConnected = true;                
    }
}

/// <summary>
/// This method is called by the Web Part infrastructure to allow the Web Part
/// to fire any initialization events
/// </summary>
public override void PartCommunicationInit()
{
    //if the web part is connected and the CellProviderInit listener is created,
    //create the args for CellProviderInit event and fire it to tell the Consumer
    //Web Part what type of cell it will be receiving when CellReady is fired later.
    if (isConnected && CellProviderInit != null)
    {
        CellProviderInitEventArgs cellProviderInitArgs = new CellProviderInitEventArgs();
        //set the field name.
        cellProviderInitArgs.FieldName = "URL";
        CellProviderInit(this, cellProviderInitArgs);
    }
}

/// <summary>
/// This is called by the Web Part infrastructure to allow the Web Part to fire any
/// of the other events from the interface (for example, CellReady). During the
/// execution of the PartCommunicationMain method, the actual communication of data
/// values takes place between Web Parts. 
/// </summary>
public override void PartCommunicationMain()
{
    //if the web part is connected and CellReady event is created,
    //create the args for this event and fire it.
    if (isConnected && CellReady != null)
    {
        CellReadyEventArgs cellReadyArgs = new CellReadyEventArgs();
        if (send_data && !string.IsNullOrEmpty(tv.SelectedValue))
        {
            //set the cell field with the selected URL
            //this field will be sent to the consumer.
            cellReadyArgs.Cell = tv.SelectedValue;
        }
        else
        {
            //nothing was selected
            cellReadyArgs.Cell = "";
        }
        CellReady(this, cellReadyArgs);
    }
}

/// <summary>
/// Register a client startup script to fixup the update panel, we need this
/// because Windows SharePoint Services JavaScript has a "form onSubmit wrapper"
/// which is used to override the default form action.
/// </summary>
private void EnsureUpdatePanelFixups()
{
    if (this.Page.Form != null)
    {
        //modify the form onSubmit wrapper
        if (this.Page.Form.Attributes["onsubmit"] == "return _spFormOnSubmitWrapper();")
        {
            this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
        }
    }
    //register the script
    ScriptManager.RegisterStartupScript(this, typeof(AjaxUrlListWP), "UpdatePanelFixup", 
      "_spOriginalFormAction = document.forms[0].action;" + 
      " _spSuppressFormOnSubmitWrapper=true;", true);
}

我们在用户展开树节点时,在运行时获取子站点。因此,我编写了一个递归方法,该方法将负责在运行时填充树状视图。

public void CreateTreeOnExpandNode(string URL, TreeNode nodeExpanded)
{
    try
    {
        SPSite site = new SPSite(URL);
        SPWeb web = null;
        if (site.Url == URL)
        {
            web = site.OpenWeb();
        }
        else
        {
            URL = URL.Replace(site.Url, "");
            if (site.ServerRelativeUrl != "/") URL = site.ServerRelativeUrl + URL;
            web = site.OpenWeb(URL);
        }
        foreach (SPWeb web1 in web.Webs)
        {
            TreeNode childnode = new TreeNode(web1.Url);
            //GetChild(web1, childnode);
            if (web1.Webs.Count > 0)
            {
                TreeNode emptyNode = new TreeNode();
                childnode.ChildNodes.Add(emptyNode);
            }
            nodeExpanded.ChildNodes.Add(childnode);
        }
    }
    catch (Exception ex)
    {
        Page.Response.Write(ex.Message);
    }
}

Consumer 部分:为了创建 Consumer 部分,我将我的控件类从 ICellConsumer 接口派生,其余部分与 Provider 部分非常相似。

为了从 Provider 部分获取数据

/// <summary>
/// Implements CellReady Event Handler. Receives data from provider
/// </summary>
/// <param name="sender">provider web part</param>
/// <param name="cellReadyArgs">arguments sent by the provider</param>
public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)
{
    if (cellReadyArgs.Cell != null)
    {
        //gets the URL from the provider and save it in ContentLink
        ContentLink = cellReadyArgs.Cell.ToString();
    }
}

#endregion

祝您 SharePoint 使用愉快!干杯!!!

历史

© . All rights reserved.