请问怎么在后台代码中怎么动态来生成服务服务器控件,可以在后台中用代码来生成服务器控件吗,我怎么做都不能在页面上生成呀,请大吓们指教!!

解决方案 »

  1.   

    下面一段代码是动态生成控件,你参考一下:
    Imports System.IO
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Public Class SubHeader
        Inherits WebControl
        'the url to navigate to,in case the user is not registered
        Private _register As String = String.Empty
        Private _back As String = String.Empty
        Sub New()
            'initialize default values
            Me.Width = New Unit(100, UnitType.Percentage)
            Me.CssClass = "SubHeader"
        End Sub    'property to allow the user to define the URL for the registration page
        Public Property RegisterURl() As String
            Get
                Return _register
            End Get
            Set(ByVal Value As String)
                _register = Value
            End Set
        End Property
        Public Property backURl() As String
            Get
                Return _back
            End Get
            Set(ByVal Value As String)
                _back = Value
            End Set
        End Property    'this method is called when the controlis being built
        Protected Overrides Sub CreateChildControls()
            'Clear any previously loaded controls
            Me.Controls.Clear()
            Dim lbl As Label        ' if the user is authenticated we will render his name
            If context.User.Identity.IsAuthenticated Then
                lbl = New Label
                lbl.Text = context.User.Identity.Name            'add the newly created label to our collection of child controls
                Me.Controls.Add(lbl)
            Else            'Otherwise,we will render a link to the registration page
                Dim reg As New HyperLink
                reg.Text = "登录"
                'if a URl isn't provided,use a default URL to the registration page
                If _register = String.Empty Then
                    reg.NavigateUrl = context.Request.ApplicationPath & _
                    Path.AltDirectorySeparatorChar & "secure" & _
                    Path.AltDirectorySeparatorChar & "login.aspx"
                Else
                    reg.NavigateUrl = _register
                End If            'add the newly created link to our collection of child controls
                Me.Controls.Add(reg)
            End If
            'add the back button
            Dim back As New HyperLink
            If _back = String.Empty Then
                back.NavigateUrl = context.Request.ApplicationPath & _
                    Path.AltDirectorySeparatorChar & "secure" & _
                    Path.AltDirectorySeparatorChar & "login.aspx"
            Else
                back.NavigateUrl = _back
            End If
            back.Text = "返回"
            Me.Controls.Add(New LiteralControl(" | "))
            Me.Controls.Add(back)        ' add a couple of blank spaces and a separator character
            Me.Controls.Add(New LiteralControl(" - "))        ' add a label with the current date
            lbl = New Label
            lbl.Text = DateTime.Now.ToLongDateString
            Me.Controls.Add(lbl)
        End SubEnd Class