← 返回所有博客文章

本文主要阐述如何继承 Studio for ASP.NET Wijmo 控件。Studio for ASP.NET Wijmo 控件提供了创建 Web 应用程序的套包,它包含了超过 40 款控件。每个控件都有其内置的属性、方法和事件用于定制控件行为。同时,也支持对控件进行继承并扩展。

但是,当用户继承 Wijmo 控件时往往会遇到以下错误:

“Microsoft JScript runtime error: Object doesn’t support property or method ‘objectName’.”

错误原因是 Wijmo 控件会根据控件类名向客户端注册 js 资源,资源名称为类名的小写形式,例如控件名称为MyCustomWijmo,在客户端 js 资源名称为:mycustomwijmo。解决方法也比较简单,代码如下:

  1: C#.NET
  2: //for C1TreeView control
  3: protected override void OnPreRender(EventArgs e)
  4:  {
  5:    base.OnPreRender(e);
  6:    RegisterScript();
  7:  }
  8:   
  9: private void RegisterScript()
 10:  {
 11:   this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.ID + "_script", "<script>$.fn.mycustomwijmo = $.fn.c1treeview</script>");
 12:   //dosomething
 13:  }

 

  1: VB.NET
  2: 'for C1TreeView control
  3: Protected Overrides Sub OnPreRender(e As EventArgs)
  4:   MyBase.OnPreRender(e)
  5:   RegisterScript()
  6: End Sub
  7:   
  8: Private Sub RegisterScript()
  9:   Me.Page.ClientScript.RegisterStartupScript(Me.[GetType](), Me.ID + "_script", "<script>$.fn.mycustomwijmo = $.fn.c1treeview></script>")
 10:   'dosomething
 11: End Sub

 

同样,你也可以使用相同的方法继承其他 Wijmo 控件:

  1: C#.NET
  2: public class CustomC1GridView : C1.Web.Wijmo.Controls.C1GridView.C1GridView
  3:  {
  4:    private void RegisterScript()
  5:    {
  6:     this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.ID + "_script", "<script>$.fn.customc1gridview = $.fn.c1gridview></script>");
  7:     //dosomething
  8:    }
  9:    protected void Page_Load(object sender, EventArgs e)
 10:    {
 11:    }
 12: }

 

  1: VB.NET
  2: Public Class CustomC1GridView
  3:      Inherits C1.Web.Wijmo.Controls.C1GridView.C1GridView
  4:  Private Sub RegisterScript()
  5:    Me.Page.ClientScript.RegisterStartupScript(Me.[GetType](), Me.ID + "_script", "<script>$.fn.customc1gridview = $.fn.c1gridview></script>")
  6:    'dosomething
  7:  End Sub
  8:  Protected Sub Page_Load(sender As Object, e As EventArgs)
  9:  End Sub
 10: End Class

 

测试 Demo:

vs2012 + Framework 4.0 + C# + Studio for ASP.NET Wijmo 点击下载