from:http://kaliking.blog.51cto.com/58641/6738
' <summary>
' 装载用户控件文件
' </summary>
' <returns></returns>
Protected Function LoadSkin() As Control
Dim skin As Control
' <summary>
' 初始化控件,并绑定控件数据
' </summary>
' <param name="skin"></param>
Protected Overridable Sub InitializeSkin(ByVal skin As Control)
End Class
End Namespace
以上功能,一定程度上可以取代.net 2.0 的master母页,甚至可以说,更加简便
原作:
《Asp.Net Forums2.0深入分析》之 Asp.Net Forums是如何实现代码分离和换皮肤的
模拟Asp.Net Forums实现可以换皮肤的控件
http://webuc.net/dotey/archive/2004/05/28/835.aspx
本人以VB代码实践了一次...
首先是基类,class1
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Namespace WebUC
' <summary>
' 换皮肤控件基类
' </summary>
<ParseChildren(True)> _
Public Class Class1: Inherits WebControl
Implements INamingContainer
' <summary>
' 换皮肤控件基类
' </summary>
<ParseChildren(True)> _
Public Class Class1: Inherits WebControl
Implements INamingContainer
Dim skinFn As String = Nothing
Public Sub New()
If (SkinFilename = Nothing) Then
SkinFilename = "wc1.ascx"
Else
SkinFilename = skinFn
End If
End Sub
If (SkinFilename = Nothing) Then
SkinFilename = "wc1.ascx"
Else
SkinFilename = skinFn
End If
End Sub
Protected Overrides Sub CreateChildControls()
Dim skin As Control
' 装载用户控件文件
skin = LoadSkin()
skin = LoadSkin()
' 初始化控件和对控件绑定
InitializeSkin(skin)
InitializeSkin(skin)
Controls.Add(skin)
End Sub
End Sub
' <summary>
' 装载用户控件文件
' </summary>
' <returns></returns>
Protected Function LoadSkin() As Control
Dim skin As Control
' 用户控件文件默认放在Themes目录下
Dim skinPath As String = SkinFilename
Dim skinPath As String = SkinFilename
' 是否定义了用户控件文件?
If SkinFilename = Nothing Then Throw New Exception("必须定义SkinFilename属性,指定用户控件文件路径")
If SkinFilename = Nothing Then Throw New Exception("必须定义SkinFilename属性,指定用户控件文件路径")
' 通过Page.LoadControl(defaultSkinPath)方法,从用户控件文件中获取 UserControl 对象
Try
skin = Page.LoadControl(skinPath)
Catch ex As Exception
Throw New Exception("用户控件文件未找到!")
End Try
Try
skin = Page.LoadControl(skinPath)
Catch ex As Exception
Throw New Exception("用户控件文件未找到!")
End Try
Return skin
End Function
End Function
' <summary>
' 初始化控件,并绑定控件数据
' </summary>
' <param name="skin"></param>
Protected Overridable Sub InitializeSkin(ByVal skin As Control)
End Sub
' <summary>
' 用户控件文件路径
' </summary>
Public Property SkinFilename() As String
Get
Return skinFn
End Get
Set(ByVal value As String)
skinFn = Value
End Set
End Property
' 用户控件文件路径
' </summary>
Public Property SkinFilename() As String
Get
Return skinFn
End Get
Set(ByVal value As String)
skinFn = Value
End Set
End Property
End Class
End Namespace
然后是个用户控件
wc1.ascx:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="wc1.ascx.vb" Inherits="wc1" %>
<asp:TextBox ID="TextBox1" runat="server">wc1</asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server">wc1</asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:CheckBox ID="CheckBox1" runat="server" />
最后是创建个页面文件,并根据SkinFilename属性加载用户控件:
page1.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="page1.aspx.vb" Inherits="page1" %>
<%@ Register TagPrefix="uc" Namespace="WebUC"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register TagPrefix="uc" Namespace="WebUC"%>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc:Class1 runat="server" SkinFilename="wc1.ascx" />
</div>
</form>
</body>
</html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc:Class1 runat="server" SkinFilename="wc1.ascx" />
</div>
</form>
</body>
</html>
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!
本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2009/10/14/1583228.html,如需转载请自行联系原作者