【发布时间】:2014-05-03 06:24:04
【问题描述】:
构建我的第一个服务器控件。在我的代码中指示的点调试中获取“值不能为空。参数名称:子”错误。文本框被创建,然后在调用父级的 page_load 事件时变为空。 page_load 事件在我的控件中的 OnInit 和 CreateChildControls 之间触发。很难找到例子。
Public Class ContactForm
Inherits WebControl
#Region "Local variables"
Private _ShowFirstName As Boolean
Private tbFirstName As TextBox
#End Region
<Browsable(True), _
Bindable(True), _
Category("Appearance"), _
DefaultValue(True), _
Localizable(True), _
Description("Show First Name field")> _
Public Property ShowFirstName() As Boolean
Get
Return _ShowFirstName
End Get
Set(value As Boolean)
_ShowFirstName = value
End Set
End Property
Protected Overrides Sub OnInit(e As EventArgs)
If ShowFirstName = True Then
Dim tbFirstName As New TextBox
tbFirstName.ID = "tbFirstName"
tbFirstName.MaxLength = 30
tbFirstName.Text = "IM HERE"
End If
MyBase.OnInit(e)
End Sub
<<<<< --- NOTE: The Page_Load event of the parent fires here ->>>>>
Protected Overrides Sub CreateChildControls()
Me.Controls.Add(tbFirstName) <<---Error: Value cannot be null. Parameter name: child
If HasChildViewState Then
ClearChildViewState()
End If
MyBase.CreateChildControls()
End Sub
End Class
【问题讨论】:
标签: asp.net custom-server-controls servercontrols