服务器控件与组建开发(2).简单得要死的控件
2-1:自定义控件
说明:继承自System.Web.UI.Control,主要是重载Render方法。把控件的html语句写出来。
文件名:SelfControl.cs
代码如下:
这些代码产生的画面如下:比较难看,赫赫。
2-2:自定义用户控件
说明:用户控件继承的类不同于以上所说的自定义控件,它继承自System.Web.UI.UserControl,而
UserControl:TemplateControl:Control,说明用户控件有了进一步的封装。
文件名:WebUserControl.ascx,WebUserControl.ascx.cs
前台代码:
后台代码:
效果:
2-3:做一个页面来调用上面两个控件。
说明:相当于一个容器,把上面两个控件装载进来。
文件名:Default.aspx
代码如下:
2-4:over。算是一个helloworld。继续学习。
2-1:自定义控件
说明:继承自System.Web.UI.Control,主要是重载Render方法。把控件的html语句写出来。
文件名:SelfControl.cs
代码如下:
using
System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace SelfControls
{
/// <summary>
/// SelfControl HelloWorld
/// </summary>
public class SelfControl : Control
{
private string _Name;
/// <summary>
/// 自定义的一个属性
/// </summary>
public string Name
{
set { _Name = value; }
get { return _Name; }
}
/// <summary>
/// 一句一句把html代码写出来.
/// 当然写的方式有多种.下机是最简单的一种.
/// 也可以用HtmlTextWriterAttribute;HtmlTextWriterStyle;HtmlTextWriterTag;
/// 定义的一些枚举值.
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<table border=1>");
writer.Write("<tr>");
writer.Write("<td>");
writer.Write("this is a self control of " + _Name);
writer.Write("</td>");
writer.Write("<td>");
writer.Write("HelloWorld!");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace SelfControls
{
/// <summary>
/// SelfControl HelloWorld
/// </summary>
public class SelfControl : Control
{
private string _Name;
/// <summary>
/// 自定义的一个属性
/// </summary>
public string Name
{
set { _Name = value; }
get { return _Name; }
}
/// <summary>
/// 一句一句把html代码写出来.
/// 当然写的方式有多种.下机是最简单的一种.
/// 也可以用HtmlTextWriterAttribute;HtmlTextWriterStyle;HtmlTextWriterTag;
/// 定义的一些枚举值.
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<table border=1>");
writer.Write("<tr>");
writer.Write("<td>");
writer.Write("this is a self control of " + _Name);
writer.Write("</td>");
writer.Write("<td>");
writer.Write("HelloWorld!");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
}
}
}
2-2:自定义用户控件
说明:用户控件继承的类不同于以上所说的自定义控件,它继承自System.Web.UI.UserControl,而
UserControl:TemplateControl:Control,说明用户控件有了进一步的封装。
文件名:WebUserControl.ascx,WebUserControl.ascx.cs
前台代码:
<%
@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl" %>
< table border ="1" >
< tr >
< td >< asp:TextBox ID ="TextBox1" runat ="server" ></ asp:TextBox ></ td >
< td >< asp:Button ID ="Button1" runat ="server" Text ="Button" /></ td >
</ tr >
</ table >
Inherits="WebUserControl" %>
< table border ="1" >
< tr >
< td >< asp:TextBox ID ="TextBox1" runat ="server" ></ asp:TextBox ></ td >
< td >< asp:Button ID ="Button1" runat ="server" Text ="Button" /></ td >
</ tr >
</ table >
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// 什么也没写
/// </summary>
public partial class WebUserControl : System.Web.UI.UserControl
{
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// 什么也没写
/// </summary>
public partial class WebUserControl : System.Web.UI.UserControl
{
}
2-3:做一个页面来调用上面两个控件。
说明:相当于一个容器,把上面两个控件装载进来。
文件名:Default.aspx
代码如下:
<%
@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% @ Register Namespace ="SelfControls" TagPrefix="cc1" %>
<% @ Register Src="~/WebUserControl.ascx" TagName = "WebUser" TagPrefix = "cc2" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > HelloWorld </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< cc1:SelfControl ID ="SelfControl1" runat ="server" Name ="" >
</ cc1:SelfControl >
< br />
< cc2:WebUser ID = "WebUser1" runat ="server" />
</ form >
</ body >
</ html >
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% @ Register Namespace ="SelfControls" TagPrefix="cc1" %>
<% @ Register Src="~/WebUserControl.ascx" TagName = "WebUser" TagPrefix = "cc2" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > HelloWorld </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< cc1:SelfControl ID ="SelfControl1" runat ="server" Name ="" >
</ cc1:SelfControl >
< br />
< cc2:WebUser ID = "WebUser1" runat ="server" />
</ form >
</ body >
</ html >
2-4:over。算是一个helloworld。继续学习。