#副標= Web Part程式開發系列(3) #大標=量身訂做您的Web Part #作者=文/林賢達 -----box----- 程式1 protected TextBox txtCaption; protected Button btnOK; -----end----- -----box----- 程式2 protected override void CreateChildControls() { base.CreateChildControls (); txtCaption=new TextBox(); this.Controls.Add(txtCaption); btnOK=new Button(); btnOK.Text=" OK "; btnOK.Click+=new EventHandler( this.btnOK_Click); this.Controls.Add(btnOK); } -----end----- -----box----- 程式3 private void btnOK_Click(object sender,EventArgs e) { if (txtCaption.Text!=string.Empty) this.Title=txtCaption.Text; } -----end----- -----box----- 程式4 protected override void RenderWebPart( HtmlTextWriter output) { this.txtCaption.RenderControl(output); this.btnOK.RenderControl(output); } -----end----- -----box----- 程式5 output.AddAttribute(HtmlTextWriterAttribute.Border, "0"); output.AddAttribute(HtmlTextWriterAttribute.Width, "100%"); output.RenderBeginTag(HtmlTextWriterTag.Table); output.RenderBeginTag(HtmlTextWriterTag.Tr); output.RenderBeginTag(HtmlTextWriterTag.Td); this.txtCaption.RenderControl(output); output.RenderEndTag(); output.AddAttribute(HtmlTextWriterAttribute.Align, "right"); output.RenderBeginTag(HtmlTextWriterTag.Td); this.btnOK.RenderControl(output); output.RenderEndTag(); output.RenderEndTag(); output.RenderEndTag(); -----end----- -----box----- 程式6 function SayHello(){ window.alert('Hello World.'); } -----end----- -----box----- 程式7 public class WebPart1 : WebPart{ private const string SCRIPTFILE = "func.js"; private const string SCRIPTKEY = "Hello"; private const string SCRIPT = @""; } -----end----- -----box----- 程式8 public WebPart1(){ this.PreRender += new EventHandler( WPClientScript_PreRender); } private void WPClientScript_PreRender(object sender , System.EventArgs e ){ RegisterCommonScript(); } protected void RegisterCommonScript() { string location = null; if (!Page.IsClientScriptBlockRegistered( SCRIPTKEY)) { location = this.ClassResourcePath + "/"; string includeScript = String.Format(SCRIPT, "javascript", location, SCRIPTFILE); Page.RegisterClientScriptBlock(SCRIPTKEY, SCRIPT); } } -----end----- -----box----- 程式9 protected override void RenderWebPart( HtmlTextWriter output) { output.Write(""); } -----end----- -----box----- 程式10 public class ProviderWebPart1 : WebPart, ICellProvider{ public event CellProviderInitEventHandler CellProviderInit; public event CellReadyEventHandler CellReady; private string myCellName="Cell Data"; } public override void EnsureInterfaces(){ RegisterInterface("ICellProvider_WPQ_", "ICellProvider", WebPart.UnlimitedConnections, ConnectionRunAt.ServerAndClient, this, "CellProviderInterface_WPQ_", "MyCellProvider", "Just a simple ICellProvider"); } -----end----- -----box----- 程式11 public override ConnectionRunAt CanRunAt(){ return ConnectionRunAt.ServerAndClient; } -----end----- -----box----- 程式12 public override void PartCommunicationConnect( string interfaceName, WebPart connectedPart,string connectedInterface, ConnectionRunAt runAt) { EnsureChildControls(); } -----end----- -----box----- 程式13 public override void PartCommunicationInit(){ CellProviderInitEventArgs providerInitArgs = new CellProviderInitEventArgs(); providerInitArgs.FieldName = myCellName; CellProviderInit(this, providerInitArgs); } -----end----- -----box----- 程式14 public override void PartCommunicationMain(){ if (CellReady != null){ CellReadyEventArgs cellReadyArgs = new CellReadyEventArgs(); cellReadyArgs.Cell = txtInput.Text; CellReady(this, cellReadyArgs); } } -----end----- -----box----- 程式15 public void CellReady(object sender, CellReadyEventArgs cellReadyArgs){ if(cellReadyArgs.Cell != null){ lblReceive.Text = cellReadyArgs.Cell.ToString(); } } -----end-----