ASP.NET 定制控件的开发(一) (2)

翻译|其它|编辑:郝浩|2005-06-20 14:17:00.000|阅读 1077 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


下面分别是测试用的完整的.aspx网页代码、网页调用的C#代码、定制控件的C#源代码,以及网页调用的VB.NET代码、定制控件的VB.NET代码:

.aspx网页代码:WebForm1.aspx


<%@ Page language="c#"
Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false"
Inherits="CustomControlWebPage.WebForm1" %>

<%@ Register TagPrefix="OReilly"
Namespace="CustomControls"
Assembly="CustomControls" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>
<HEAD>

</HEAD>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">

<asp:Button Runat="server"
Text="Increase Size"
OnClick="Button1_Click"
id="Button1" />

<OReilly:WebCustomControl1
Runat="Server"
Text="Hello World!"
id="WC1" />

</FORM>
</body>
</HTML>


定制控件的C#源代码: WebCustomControl1.cs


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomControls
{
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
   private string text;

    // 构造器初始化ViewState中的值
   public WebCustomControl1( )
   {
       ViewState["Size"] = "1";
    }

   // 由VS.NET创建的代码
   [Bindable(true),
   Category("Appearance"),
   DefaultValue("")]
   public string Text
   {
     get { return text; }
     set{ text = value; }
   }

    // 在ViewState中保存Size的自定义属性
  public int Size
  {
       get { return Convert.ToInt32((string) ViewState["Size"]); }
       set { ViewState["Size"] = value.ToString( ); }
   }

     // Render负责输出字体的大小

protected override void Render(HtmlTextWriter output)
  {
    output.Write("" +
    Text + "");
    }
  }
}



网页调用的VB.NET代码: WebForm1.aspx.vb


Imports CustomControls.WebCustomControl1

Public Class WebForm1
Inherits System.Web.UI.Page

Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WC1 As VBCustomControls.WebCustomControl1

Public Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
WC1.Size += 1
End Sub
End Class


定制控件的VB.NET代码: WebCustomControl1.vb


Imports System.ComponentModel
Imports System.Web.UI
Imports System.Drawing

runat=server>")> _
Public Class WebCustomControl1
Inherits System.Web.UI.WebControls.WebControl

Dim _text As String

Public Sub WebCustomControl1( )
ViewState("Size") = "1"
End Sub

_
Property [Text]( ) As String
Get
Return _text
End Get

Set(ByVal Value As String)
_text = Value
End Set
End Property

Protected Overrides Sub Render( _
ByVal output As System.Web.UI.HtmlTextWriter)
output.Write("" & [Text] & "")
End Sub

Public Property Size( ) As Integer
Get
    Return Convert.ToInt32(ViewState("Size"))
End Get
    Set(ByVal Value As Integer)
    ViewState("Size") = Value.ToString( )
End Set
End Property

End Class


为了演示点击按钮的效果,在下面的图中我们创建了该程序的二个实例,在第二个实例中我们按了三次按钮:





每次点击按钮,状态变量Size都会增加;当绘制网页时,状态变量Size就被用来设置文本的大小。
 


派生控件的创建


在很多情况下,我们并非一定要从零开始创建控件。如果只是想简单地扩展现有控件的功能,我们可以象继承其他类那样通过继承一个现有的控件得到新的控件。

例如,也许我们希望维持按钮被点击的次数,这样的按钮在许多应用程序中都非常有用,但不幸的是,web Button控件没有提供这种功能。

为了克服这种按钮控件在功能方面的不足,我们可以从System.Web.UI.WebControls.Button控件中派生生成新的定制控件,下面分别是用C#和VB.NET生成的控件的代码:

用C#实现的CountedButton

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomControls
{
    // 从System.Web.UI.WebControls.Button控件中派生的定制控件
   
public class CountedButton : System.Web.UI.WebControls.Button
    {

    // 构造器初始化ViewState变量
   public CountedButton( )
   {
     this.Text = "Click me";
     ViewState["Count"] = 0;
   }
   //存取ViewState中的Count值
public int Count
{
    get
    {
      return (int) ViewState["Count"];
     }

    set
    {
        ViewState["Count"] = value;
   }
}

// 覆盖OnClick,增加count,更新按钮文本,然后调用基础类的方法
protected override void OnClick(EventArgs e)
{
    ViewState["Count"] = ((int)ViewState["Count"]) + 1;
    this.Text = ViewState["Count"] + " clicks";
    base.OnClick(e);
    }
  }
}


用VB.NET实现的CountedButton


Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls

' 从System.Web.UI.WebControls.Button控件中派生的定制控件
Public Class CountedButton
Inherits System.Web.UI.WebControls.Button

' 构造器初始化ViewState变量
Public Sub New( )
   Me.Text = "Click me"
   ViewState("Count") = 0
End Sub

' 存取ViewState中的Count值
Public Property Count( ) As Integer
   Get
      Return CInt(ViewState("Count"))
   End Get
   Set(ByVal Value As Integer)
   ViewState("Count") = Value
   End Set
End Property

'覆盖OnClick,增加count,更新按钮文本,然后调用基础类的方法
Protected Overrides Sub OnClick(ByVal e As EventArgs)
ViewState("Count") = CInt(ViewState("Count")) + 1
Me.Text = ViewState("Count") & " clicks"
MyBase.OnClick(e)
End Sub
End Class

我们首先从现有的Button类派生出一个新的类:
public class CountedButton : System.Web.UI.WebControls.Button

在VB.NET中的代码为:

Public Class CountedButton
Inherits System.Web.UI.WebControls.Button

这个类的用途是维持其状态:即?被按下过多少次。在新类中,我们提供了一个public性质的特性Count,Count是由ViewState中存储的一个值而不是由局部变量提供支持的,这是必要的,因为按钮将显示在网页上,否则其状态就会丢失。使用C#语言定义的Count特性如下所示:
public int Count
{
   get
    {
     return (int) ViewState["Count"];
    }

   set
   {
     ViewState["Count"] = value;
   }
}


使用VB.NET语言定义的Count特性:


Public Property Count( ) As Integer
Get
  Return CInt(ViewState("Count"))
End Get
  Set(ByVal Value As Integer)
  ViewState("Count") = Value
End Set
End Property


我们使用字符串“Count”作为偏移量在ViewState集合中获得Count值,返回的是一个对象,在C#中的类型为int,在VB.NET中的类型为Integer

为了确保返回的特性值是一个有效值,我们在构造器中对Count特性进行初始化。在C#中构造器代码是:

public CountedButton( )
{
   this.Text = "Click me";
   ViewState["Count"] = 0;
}



在VB.NET中,构造器的代码是:

Public Sub New( )
Me.Text = "Click me"
ViewState("Count") = 0
End Sub


由于CountedButton是由Button派生得出的,因此要覆盖Click事件也就非常简单。当用户点击按钮时,保存在ViewState中的Count值就会增加,按钮上的文本也会更新,反映新的Count值。然后再调用基础类中的OnClick方法。用C#编写的点击按钮的事件处理程序为:

protected override void OnClick(EventArgs e)
{
    ViewState["Count"] = ((int)ViewState["Count"]) + 1;
    this.Text = ViewState["Count"] + " clicks";
    base.OnClick(e);
}


用VB.NET编写的点击按钮的事件处理程序为:

Protected Overrides Sub OnClick(ByVal e As EventArgs)
ViewState("Count") = CInt(ViewState("Count")) + 1
Me.Text = ViewState("Count") & " clicks"
MyBase.OnClick(e)
End Sub


将该控件添加到.aspx表单上:

<OReilly:CountedButton Runat="Server" id="CB1" />


我们无需再添加额外的Register命令,因为该控件与定制控件一样,都是在CustomControls名字空间和组合体中的。

当点击该按钮4次时,按钮上就会显示出点击的次数,如下图所示:



 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP