2011-12-15 5 views
0

私の問題を説明する方法がわかりません WebパーツをWebパーツと組み合わせて実装しようとしています。接続にはIWebPartRowインターフェイスを使用します。 ここに私が達成したいことがあります: リストビューのwebpartでは、アイテムを選択できます。 WebPartの接続プロバイダからの詳細とボタンが表示されます。このボタンをクリックすると、接続プロバイダーからのデータを使用して何かしたいと思います。ここでボタンをクリックしたときにIWebPartRow接続からデータを使用する方法。プロバイダは常にnullです

Webパーツコード:

using System; 
using System.ComponentModel; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.WebControls; 
using System.Data; 
using System.Text; 

namespace ConnectedFilesWebPart.TestWebPart 
{ 
    [ToolboxItemAttribute(false)] 
    public class TestWebPart : Microsoft.SharePoint.WebPartPages.WebPart 
    { 

     public DataRowView DataRow 
     { 
      get; 
      set; 
     } 

     public IWebPartRow Provider 
     { 
      get; 
      set; 
     } 
     private readonly StringBuilder _sb = new StringBuilder(); 

     protected override void CreateChildControls() 
     { 
      //TestWebPartUserControl control = (TestWebPartUserControl)Page.LoadControl(_ascxPath); 
      Button myButton = new Button(); 
      myButton.Text = "myButton"; 
      myButton.Click += new EventHandler(myButton_Click); 
      Controls.Add(myButton); 
     } 

     protected void myButton_Click(object sender, EventArgs e) 
     { 

      if (DataRow != null) 
      { 
       int lookupId = Int32.Parse(DataRow["ID"].ToString()); 
       string title = DataRow["Title"].ToString(); 
      } 
     } 

     private void GetRowData(object rowData) 
     { 
      try 
      { 
       DataRow = (DataRowView)rowData; 
      } 
      catch 
      { 
       DataRow = null; 
      } 
     } 

     protected override void OnPreRender(EventArgs e) 
     { 
      if (Provider != null) 
       Provider.GetRowData(new RowCallback(GetRowData)); 
     } 

     [ConnectionConsumer("Row", AllowsMultipleConnections = true)] 
     public void ReceiveProvider(IWebPartRow p) 
     { 
      Provider = p; 
     } 

     protected override void RenderContents(HtmlTextWriter writer) 
     { 
      base.RenderContents(writer); 
      if (DataRow != null) 
      { 
       DataRow providerRow = DataRow.Row; 
       foreach (DataColumn column in providerRow.Table.Columns) 
        _sb.AppendFormat("- data column: {0} = {1}\n", column.ColumnName ?? "n/a", providerRow[column]); 
       _sb.Append("\n"); 
       writer.Write(_sb.ToString().Replace("\n", "<br>")); 
      } 
     } 
    } 
} 

Webパーツがロードされると、接続が正常に動作します。しかし、何らかの理由で、myButton_Clickのボタンをクリックすると、DataRowオブジェクトが常にnullになります。あたかもwebpartが再びロードされたように見えますが、今回はプロバイダが呼び出されていないようです。私は間違って何をしていますか?

答えて

0

私は同じことをしようとしています。私はtutorialをMSDN上に見つけました。それは、あなたと私のためにドットを接続するように見えます。お役に立てれば。

編集:さらに良い答えはhereです。それはあなたがしようとしているものに似た何かをしており、それは動作します!ダウンロード可能なコードもあります!

関連する問題