利用MS AJAX 扩展服务器端控件1
点击次数:29 次 发布日期:2008-11-26 11:06:12 作者:源代码网
|
源代码网推荐 实例代码:IScriptControl.rar 源代码网推荐 一、创建网站,选择ASP.NET AJAX-Enabled Web Site. 源代码网推荐 二、向项目中添加一个类,使其派生自TextBox,并实现IScriptControl接口。如下代码实例: 源代码网推荐 源代码网推荐 源代码网推荐 public class SampleTextBox : TextBox, IScriptControl 源代码网推荐 源代码网推荐 三、这个控件我们将实现两个属性: 源代码网推荐 HighlightCSSClass 控件得到焦点后的样式。当控件得到焦点的时候使其能够高亮显示。 源代码网推荐 NoHighlightCssClass 失去焦点的控件的样式。 源代码网推荐 源代码网推荐 源代码网推荐 public string HighlightCssClass 源代码网推荐 { 源代码网推荐 get { return _highlightCssClass; } 源代码网推荐 set { _highlightCssClass = value; } 源代码网推荐 } 源代码网推荐 源代码网推荐 public string NoHighlightCssClass 源代码网推荐 { 源代码网推荐 get { return _noHighlightCssClass; } 源代码网推荐 set { _noHighlightCssClass = value; } 源代码网推荐 } 源代码网推荐 源代码网推荐 四、接口IScriptControl 的实现。 源代码网推荐 GetScriptDescriptors() 返回一个包含控件客户端实例的属性和事件句柄的 ScriptDescriptor 类型的数组。 源代码网推荐 GetScriptReferences() 返回一个包含控件客户端 javascript 代码的ScriptReference 类型的数组。 源代码网推荐 在这个实例中,我们用四个函数来实现这两个函数。代码入下: 源代码网推荐 protected virtual IEnumerable<ScriptReference> GetScriptReferences() 源代码网推荐 { 源代码网推荐 ScriptReference reference = new ScriptReference(); 源代码网推荐 reference.Path = ResolveClientUrl("SampleTextBox.js"); 源代码网推荐 源代码网推荐 return new ScriptReference[] { reference }; 源代码网推荐 } 源代码网推荐 源代码网推荐 protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors() 源代码网推荐 { 源代码网推荐 ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Samples.SampleTextBox", this.ClientID); 源代码网推荐 descriptor.AddProperty("highlightCssClass", this.HighlightCssClass); 源代码网推荐 descriptor.AddProperty("nohighlightCssClass", this.NoHighlightCssClass); 源代码网推荐 源代码网推荐 return new ScriptDescriptor[] { descriptor }; 源代码网推荐 } 源代码网推荐 源代码网推荐 IEnumerable<ScriptReference> IScriptControl.GetScriptReferences() 源代码网推荐 { 源代码网推荐 return GetScriptReferences(); 源代码网推荐 } 源代码网推荐 源代码网推荐 IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors() 源代码网推荐 { 源代码网推荐 return GetScriptDescriptors(); 源代码网推荐 } 五、这册控件。代码比较简单,所以就不再多加讲述,入下: 源代码网推荐 protected override void OnPreRender(EventArgs e) 源代码网推荐 { 源代码网推荐 if (!this.DesignMode) 源代码网推荐 { 源代码网推荐 // Test for ScriptManager and register if it exists 源代码网推荐 sm = ScriptManager.GetCurrent(Page); 源代码网推荐 源代码网推荐 if (sm == null) 源代码网推荐 throw new HttpException("A ScriptManager control must exist on the current page."); 源代码网推荐 源代码网推荐 sm.RegisterScriptControl(this); 源代码网推荐 } 源代码网推荐 源代码网推荐 base.OnPreRender(e); 源代码网推荐 } 源代码网推荐 源代码网推荐 protected override void Render(HtmlTextWriter writer) 源代码网推荐 { 源代码网推荐 if (!this.DesignMode) 源代码网推荐 sm.RegisterScriptDescriptors(this); 源代码网推荐 源代码网推荐 base.Render(writer); 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
