动态加载用户控件----以用户控件的方式达到代码分离的目的[转]

  1. 云栖社区>
  2. 博客>
  3. 正文

动态加载用户控件----以用户控件的方式达到代码分离的目的[转]

桃子红了呐 2017-11-09 13:34:00 浏览723
展开阅读全文
from:http://kaliking.blog.51cto.com/58641/6738

原作:
《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
Namespace WebUC
    ' <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
        Protected Overrides Sub CreateChildControls()
            Dim skin As Control
            ' 装载用户控件文件
            skin = LoadSkin()
            ' 初始化控件和对控件绑定
            InitializeSkin(skin)
            Controls.Add(skin)
        End Sub

        ' <summary>
        ' 装载用户控件文件
        ' </summary>
        ' <returns></returns>
        Protected Function LoadSkin() As Control
            Dim skin As Control
            ' 用户控件文件默认放在Themes目录下
            Dim skinPath As String = 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
            Return skin
        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

    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" />
 
最后是创建个页面文件,并根据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">
<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>
 
 
以上功能,一定程度上可以取代.net 2.0 的master母页,甚至可以说,更加简便
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!



















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2009/10/14/1583228.html,如需转载请自行联系原作者




网友评论

登录后评论
0/500
评论