ASP.NET创建服务器控件示例代码

80酷酷网    80kuku.com

  创作您自己的 ASP.NET 服务器控件很容易。创建简单的自定义控件时,您所要做的只是定义从 System.Web.UI.Control 派生的类并重写它的 Render 方法。Render 方法采用 System.Web.UI.HtmlTextWriter 类型的参数。控件要发送到客户端的 HTML 作为字符串参数传递到 HtmlTextWriter 的 Write 方法。
例如:
服务器控件代码(简单显示字符串):Simple.cs:

以下是Simple.cs:
Imports System 
Imports System.Web 
Imports System.Web.UI 
Namespace SimpleControlSamples 
public class SimpleCSharp
{
    
    protected override void Render(HtmlTextWriter Output)
    {
      Response.Write("<H2>欢迎来到Asp.net源码下载专业站<H2>");
    }
}

引用文件Simple.aspx:

以下是Simple.aspx:
<% Register TagPrefix="SimpleControlSamples" Namespace="SimpleControlSamples" Assembly="SimpleControlSamplesCSharp" %> 
<html> 
<body> 
<form method="POST" action="Simple.aspx" runat=server> 
<SimpleControlSamples:SimpleCSharp id="MyControl" runat=server/> 
</form> 
</body> 
</html> 

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: