Asp.net 复合控件:不是已知元素
我发现了一篇优秀的帖子(由jdavies撰写),可以在按钮单击此处时动态添加一个新文本框: 因为这正是我想要做的,所以我在使用c#的asp.net项目中尝试了这一点,效果很好。我已经将其翻译成vb.net,因为我在阅读c#时遇到了问题 这是控件:Asp.net 复合控件:不是已知元素,asp.net,vb.net,Asp.net,Vb.net,我发现了一篇优秀的帖子(由jdavies撰写),可以在按钮单击此处时动态添加一个新文本框: 因为这正是我想要做的,所以我在使用c#的asp.net项目中尝试了这一点,效果很好。我已经将其翻译成vb.net,因为我在阅读c#时遇到了问题 这是控件: Imports System.Collections.Generic Imports System.Linq Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebContr
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace ASPNetWebApplication.Controls
Public Class TextBoxCollection
Inherits CompositeControl
Private Property TextBoxes() As List(Of String)
Get
If ViewState("TextBoxes") Is Nothing Then
ViewState("TextBoxes") = New List(Of String)()
End If
Return DirectCast(ViewState("TextBoxes"), List(Of String))
End Get
Set(value As List(Of String))
ViewState("TextBoxes") = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
For Each textBox As String In TextBoxes
Controls.Add(New TextBox() With { _
.ID = textBox _
})
Controls.Add(New Literal() With { _
.Text = "<br/>" _
})
Next
End Sub
Public Function GetTextBox(id As String) As TextBox
Return DirectCast(Controls.Cast(Of Control)().Where(Function(t) (If(t.ID Is Nothing, "", t.ID.ToLower())) = id.ToLower()).SingleOrDefault(), TextBox)
End Function
Public Sub AddTextBox(id As String)
TextBoxes.Add(id)
End Sub
End Class
End Namespace
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="ASPNetWebApplication._Default" %>
<%@ Register Assembly="ASPNetWebApplication" Namespace="ASPNetWebApplication.Controls" TagPrefix="ASPNetWebApplication" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<ASPNetWebApplication:TextBoxCollection ID="TextBoxCollection1" runat="server" />
<br />
<asp:Button ID="AddTextBoxesButton" runat="server" OnClick="AddTextBoxesButton_Click" Text="Add Some Text Boxes" />
<asp:Button ID="GetTextBoxValuesButton" runat="server" OnClick="GetTextBoxValuesButton_Click" Text="Get TextBox Values" Visible="false" />
<br />
<asp:Literal ID="TextBoxEntriesLabel" runat="server"></asp:Literal>
</asp:Content>
Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub AddTextBoxesButton_Click(sender As Object, e As EventArgs)
For i As Integer = 0 To 9
TextBoxCollection1.AddTextBox(String.Format("TextBox{0}", i))
Next
AddTextBoxesButton.Visible = False
GetTextBoxValuesButton.Visible = True
End Sub
Protected Sub GetTextBoxValuesButton_Click(sender As Object, e As EventArgs)
TextBoxEntriesLabel.Text = String.Empty
For i As Integer = 0 To 9
Dim textBoxId As String = String.Format("TextBox{0}", i)
TextBoxEntriesLabel.Text += String.Format("TextBox: {0}, Value: {1}<br/>", textBoxId, TextBoxCollection1.GetTextBox(textBoxId).Text)
Next
End Sub
End Class
导入System.Collections.Generic
导入系统
导入系统.Web
导入System.Web.UI
导入System.Web.UI.WebControl
命名空间ASPNetWebApplication.Controls
公共类TextBoxCollection
继承CompositeControl
私有属性textboxs()作为列表(字符串)
收到
如果ViewState(“文本框”)为空,则
ViewState(“文本框”)=新列表(字符串)()
如果结束
返回DirectCast(视图状态(“文本框”),列表(字符串))
结束
设置(值为列表(字符串))
ViewState(“文本框”)=值
端集
端属性
受保护的覆盖子CreateChildControls()
将每个文本框作为文本框中的字符串
控件添加(带有{_
.ID=文本框_
})
Add(New Literal()和{_
.Text=“
”_
})
下一个
端接头
公共函数GetTextBox(id为字符串)作为TextBox
返回DirectCast(Controls.Cast(控件的))其中(函数(t)(如果(t.ID为Nothing,“,t.ID.ToLower())=ID.ToLower()).SingleOrDefault(),TextBox)
端函数
公共子AddTextBox(id为字符串)
文本框。添加(id)
端接头
末级
结束命名空间
标记:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace ASPNetWebApplication.Controls
Public Class TextBoxCollection
Inherits CompositeControl
Private Property TextBoxes() As List(Of String)
Get
If ViewState("TextBoxes") Is Nothing Then
ViewState("TextBoxes") = New List(Of String)()
End If
Return DirectCast(ViewState("TextBoxes"), List(Of String))
End Get
Set(value As List(Of String))
ViewState("TextBoxes") = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
For Each textBox As String In TextBoxes
Controls.Add(New TextBox() With { _
.ID = textBox _
})
Controls.Add(New Literal() With { _
.Text = "<br/>" _
})
Next
End Sub
Public Function GetTextBox(id As String) As TextBox
Return DirectCast(Controls.Cast(Of Control)().Where(Function(t) (If(t.ID Is Nothing, "", t.ID.ToLower())) = id.ToLower()).SingleOrDefault(), TextBox)
End Function
Public Sub AddTextBox(id As String)
TextBoxes.Add(id)
End Sub
End Class
End Namespace
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="ASPNetWebApplication._Default" %>
<%@ Register Assembly="ASPNetWebApplication" Namespace="ASPNetWebApplication.Controls" TagPrefix="ASPNetWebApplication" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<ASPNetWebApplication:TextBoxCollection ID="TextBoxCollection1" runat="server" />
<br />
<asp:Button ID="AddTextBoxesButton" runat="server" OnClick="AddTextBoxesButton_Click" Text="Add Some Text Boxes" />
<asp:Button ID="GetTextBoxValuesButton" runat="server" OnClick="GetTextBoxValuesButton_Click" Text="Get TextBox Values" Visible="false" />
<br />
<asp:Literal ID="TextBoxEntriesLabel" runat="server"></asp:Literal>
</asp:Content>
Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub AddTextBoxesButton_Click(sender As Object, e As EventArgs)
For i As Integer = 0 To 9
TextBoxCollection1.AddTextBox(String.Format("TextBox{0}", i))
Next
AddTextBoxesButton.Visible = False
GetTextBoxValuesButton.Visible = True
End Sub
Protected Sub GetTextBoxValuesButton_Click(sender As Object, e As EventArgs)
TextBoxEntriesLabel.Text = String.Empty
For i As Integer = 0 To 9
Dim textBoxId As String = String.Format("TextBox{0}", i)
TextBoxEntriesLabel.Text += String.Format("TextBox: {0}, Value: {1}<br/>", textBoxId, TextBoxCollection1.GetTextBox(textBoxId).Text)
Next
End Sub
End Class
和代码隐藏:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace ASPNetWebApplication.Controls
Public Class TextBoxCollection
Inherits CompositeControl
Private Property TextBoxes() As List(Of String)
Get
If ViewState("TextBoxes") Is Nothing Then
ViewState("TextBoxes") = New List(Of String)()
End If
Return DirectCast(ViewState("TextBoxes"), List(Of String))
End Get
Set(value As List(Of String))
ViewState("TextBoxes") = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
For Each textBox As String In TextBoxes
Controls.Add(New TextBox() With { _
.ID = textBox _
})
Controls.Add(New Literal() With { _
.Text = "<br/>" _
})
Next
End Sub
Public Function GetTextBox(id As String) As TextBox
Return DirectCast(Controls.Cast(Of Control)().Where(Function(t) (If(t.ID Is Nothing, "", t.ID.ToLower())) = id.ToLower()).SingleOrDefault(), TextBox)
End Function
Public Sub AddTextBox(id As String)
TextBoxes.Add(id)
End Sub
End Class
End Namespace
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="ASPNetWebApplication._Default" %>
<%@ Register Assembly="ASPNetWebApplication" Namespace="ASPNetWebApplication.Controls" TagPrefix="ASPNetWebApplication" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<ASPNetWebApplication:TextBoxCollection ID="TextBoxCollection1" runat="server" />
<br />
<asp:Button ID="AddTextBoxesButton" runat="server" OnClick="AddTextBoxesButton_Click" Text="Add Some Text Boxes" />
<asp:Button ID="GetTextBoxValuesButton" runat="server" OnClick="GetTextBoxValuesButton_Click" Text="Get TextBox Values" Visible="false" />
<br />
<asp:Literal ID="TextBoxEntriesLabel" runat="server"></asp:Literal>
</asp:Content>
Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub AddTextBoxesButton_Click(sender As Object, e As EventArgs)
For i As Integer = 0 To 9
TextBoxCollection1.AddTextBox(String.Format("TextBox{0}", i))
Next
AddTextBoxesButton.Visible = False
GetTextBoxValuesButton.Visible = True
End Sub
Protected Sub GetTextBoxValuesButton_Click(sender As Object, e As EventArgs)
TextBoxEntriesLabel.Text = String.Empty
For i As Integer = 0 To 9
Dim textBoxId As String = String.Format("TextBox{0}", i)
TextBoxEntriesLabel.Text += String.Format("TextBox: {0}, Value: {1}<br/>", textBoxId, TextBoxCollection1.GetTextBox(textBoxId).Text)
Next
End Sub
End Class
Public类\u默认值
继承页面
受保护的子页_Load(ByVal sender作为对象,ByVal e作为事件args)处理Me.Load
端接头
受保护的子AddTextBoxesButton_Click(发件人作为对象,e作为事件参数)
对于i,整数=0到9
TextBoxCollection1.AddTextBox(String.Format(“TextBox{0}”,i))
下一个
AddTextBoxesButton.Visible=False
GetTextBoxValuesButton.Visible=True
端接头
受保护的子GetTextBoxValuesButton_单击(发件人作为对象,e作为事件参数)
TextBoxEntriesLabel.Text=String.Empty
对于i,整数=0到9
Dim textBoxId作为String=String.Format(“TextBox{0}”,i)
TextBoxEntriesLabel.Text+=String.Format(“TextBox:{0},值:{1}
”,textBoxId,TextBoxCollection1.GetTextBox(textBoxId).Text)
下一个
端接头
末级
我试着做完全相同的事情来学习它是如何工作的。但是在vb.net中,我在default.aspx中得到了一个警告:
“元素“TextBoxCollection”不是已知元素。”
以及default.aspx.designer中的错误:
“不允许输入'ASPNetWebApplication.Controls.TextBoxCollection'。”
定义的。”
这一错误的原因可能是什么
(这是我关于Stackoverflow的第一个问题,我不是一个好的程序员,但我想学习它。因此,请为我简单的问题(以及我蹩脚的英语)道歉。我非常感谢一些提示。)我相信VB.NET中的
Namespace
语句创建了一个相对于项目根命名空间的命名空间名称。尝试使用NamespaceControls
而不是NamespaceASPNetWebApplication。Controls
和/或使用ILSpy或类似工具检查生成的dll,以查看已生成的命名空间。我怀疑您错过了您提到的帖子中的注释:“注意:将Assembly=“ASPNetWebApplication”更改为程序集的名称。”@安德鲁·莫顿:谢谢。我将程序集命名为“aspnetebapplication”,只是为了使所有内容与示例完全相同。项目文件:“ASPNetWebApplication.vbproj”程序集名称:“ASPNetWebApplication”根命名空间:“ASPNetWebApplication”我相信VB.NET中的namespace语句会创建一个相对于项目根命名空间的命名空间名称。请尝试使用名称空间控件
而不是名称空间aspnetweapplication。控件
和/或使用ILSpy或类似工具检查生成的dll,以查看已生成的名称空间。@Joe:太好了!这就是全部,它现在起作用了。非常感谢你!当您创建此问题的答案时,我将标记它。