2. 選擇陣列式方案
3. 新增一個 WebPart
4. 在畫面上放上一個TextBox跟一個Button,撰寫以下的程式碼
[ToolboxItemAttribute(false)]
public class WebPart1 : WebPart
{
TextBox txt;
Button btn;
protected override void CreateChildControls()
{
txt = new TextBox();
btn = new Button();
btn.Text = "Click";
this.Controls.Add(txt);
this.Controls.Add(btn);
}
}
5. 建立另一個 WebPart2 ,來接收 WebPart1 的資料
6. 在 WebPart2 放入一個 Label ,準備接收 WebPart1 的 TextBox傳過來的值
Label lbl;
protected override void CreateChildControls()
{
lbl = new Label();
this.Controls.Add(lbl);
}
7. 接下來要讓兩個 WebPart 可以做 Connection,主要是要建立一個 Interface ,兩者透過這個 Inteface 來傳送資料
8. 首先新增一個介面
9. 在 Interface 中撰寫以下程式碼
public interface IData
{
string Data { get; }
}
10. 定義提供者( WebPart1 ),將提供者實作 IData 這個 Interface,並且要實作 IData 的屬性
將 WebPart 的程式碼修改如下
[ToolboxItemAttribute(false)]
public class WebPart1 : WebPart,IData
{
TextBox txt;
Button btn;
protected override void CreateChildControls()
{
txt = new TextBox();
btn = new Button();
btn.Text = "Click";
this.Controls.Add(txt);
this.Controls.Add(btn);
}
public string Data
{
get
{
EnsureChildControls();
return txt.Text;
}
}
}
11. 提供者( WebPart1 )要加入 ProviderInterface的方法回傳介面,並且加上 ConnectionProvider的屬性
在 WebPart1 加上以下程式碼
[ConnectionProvider("文字資料")]
public IData ProvideInterface()
{
return this;
}
12. 最後再消費者( WebPart2 )加上屬性來接收資料
[ConnectionConsumer("文字資料")]
public void GetInterface(IData myInterface)
{
EnsureChildControls();
lbl.Text = myInterface.Data;
}
13. 將專案佈署到網頁上
14. 設定 WebPart 之間的連線
15. 結果
沒有留言:
張貼留言