概念
1. 构建自定义控件
目的:创建类似MS自带的控件,具有属性、方法、事件等特性,利于重用。一、在类中写自定义控件
自定义DropDownList:
1,首先创建一个cs类文件Controls(采用工厂模式)
using System.Collections.Generic;
using System.Text;
using System;
using System.IO;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Configuration;
using Model;
using BLL;
namespace Controls
{
public class useDepDropDownList : DropDownList
{
//不带Session的自定义控件
public useDepDropDownList()
{
this.Items.Add(new ListItem("全部部门", " "));
foreach (DepInfo cat in Dep.GetDeps())
{
this.Items.Add(new ListItem(cat.Name, cat.Name));
}
}
}
public class CompanyDepDropDownList : DropDownList
{
//带Session的自定义控件
public CompanyDepDropDownList()
{
if (Context.Session["username"] != null)
{
this.Items.Add(new ListItem("全部", "全部"));
foreach (GWInfo cat in GW.GetDepByloginid(Context.Session["username"].ToString()))
{
this.Items.Add(new ListItem(cat.dep.ToString(), cat.dep.ToString()));
}
}
}
}
}
2,创建一个页面aspx页面使用自定义的控件来绑定下拉框的数据
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Defaulthc.aspx.cs" Inherits="Import_Defaulthc" %>
<%@ Register Assembly="Controls" Namespace="Controls" TagPrefix="Hc" %>
<!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>自定义控件绑定部门</title>
</head>
<body>
<form id="form1" runat="server">
<div>
部门<Hc:useDepDropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
Width="120px">
</Hc:useDepDropDownList>
</div>
</form>
</body>
</html>
这样就不用在UI页面的aspx.cs里写任何代码,而且自定义控件可重复使用省去很多重复代码
二、还有一种方法vs里提供了自定义Web用户控件(.ascx)
同样在你的自定义控件.ascx.cs内写入方法,和上述相同
public class useDepDropDownList : DropDownList
{
//不带Session的自定义控件
public useDepDropDownList()
{
this.Items.Add(new ListItem("全部部门", " "));
foreach (DepInfo cat in Dep.GetDeps())
{
this.Items.Add(new ListItem(cat.Name, cat.Name));
}
}
}
在调用它的 .aspx文件中同样需要在最上边加入自定义命名:
首先要注册自定义控件,注册的语句要写到页面的上方(属于页面指令的位置。 )
<%@ Register TagPrefix="可自定义的控件前缀" TagName="自定义的控件标签名" Src="自定义控件的位置"%>
<%@ Register src="MyDDL.ascx" TagName="useDepDropDownList" TagPrefix="Hc" %>
然后在aspx文件中使用:
<自定义的控件前缀:自定义的控件标签名 ID="yourControl" runat="server"></前缀:控件标签名>
<Hc:useDepDropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
Width="120px">
</Hc:useDepDropDownList>
使用自定义控件的属性在 .aspx.cs文件中调用就可以了:
还可以在ascx中自己设计一个模版块,在aspx中引用,ascx.cs后台可以写任意方法
.ascx代码
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserLeftControl.ascx.cs" Inherits="Controls_UserLeftControl" %> <div style="text-align:left;"> <div> <h3>登录用户信息</h3> <ul> <li>用户名:<span class="red"></span></li> <li>称呼:<span ></span></li> <li>性别:<span ></span></li> <li>注册时间:<span></span></li> <li>[<a style="color:Red" href="~/index.aspx" >回到首页</a>]</li> </ul> </div> <div class="menber_link"> <h3>管理中心</h3> <ul> <li><a href="#">我的资料</a></li> <li><a href="#">我的收货地址</a></li> <li><a href="#">修改密码</a></li> <li><a href="#">我的订单</a></li> <li><a href="#">我的积分</a></li> <li><a href="#">我的收藏</a></li> <li><a href="#">我的购物车</a></li> <li><a href="#">投诉/建议/咨询</a></li> <li><asp:LinkButton ID="linkbin" CausesValidation="false" runat="server">安全退出</asp:LinkButton></li> </ul> </div> </div>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="text1.aspx.cs" Inherits="text1" %> <%@ Register src="~/Controls/UserLeftControl.ascx" TagName="UserLeftControl" TagPrefix="Hc" %> <!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 id="Head1" runat="server"> <title>Untitled Page</title> <link href="~/css/UserLeft.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="from1" runat="server"> <div runat="server"> <Hc:UserLeftControl runat="server"></Hc:UserLeftControl> </div> </form> </body> </html>
将编译好的dll添加到工具箱内
在工具栏下面找个空白的地方点击右键-->添加选项卡-->选择项-->浏览-->找到自己编译好的dll-->确定,这时候就能在工具箱的自定义选项卡内看到自己的控件了.
http://msdn.microsoft.com/zh-cn/library/yhzc935f(VS.80).aspx
http://www.cnblogs.com/holywolf/archive/2008/12/23/1360316.html
http://blog.csdn.net/gujia200808/article/details/5403437
注册AssemblyInfo.cs
http://blog.bossma.cn/dotnet/aspnet-custom-control-develop-2/
注:欢迎喜爱编程的朋友进群交流。QQ群交流:256169347
群共享了很多pdf书籍文档