复合控制:不是已知元素

时间:2014-05-11 13:12:15

标签: asp.net vb.net

我找到了一个优秀的帖子(通过jdavies)来动态添加一个新的文本框,点击此处按钮:https://stackoverflow.com/a/7677202/3620572

因为这正是我想要做的,所以我在使用c#的asp.net项目中尝试了这个,并且它工作正常。我把它翻译成了vb.net,因为我在阅读c#时遇到了问题。

这是控件

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

我尝试做同样的事情来学习它是如何工作的。但在vb.net中,我在default.aspx中收到了警告:

  

“元素'TextBoxCollection'不是已知元素。”

和default.aspx.designer中的错误:

  

“输入'ASPNetWebApplication.Controls.TextBoxCollection'不是   定义的“。

出现这种错误的原因是什么?

(这是我关于Stackoverflow的第一个问题,我不是一个优秀的程序员,但我想学习它。因此,请为我的不成熟的问题道歉(我的英语也很糟糕)。我真的很感激一些提示。)

1 个答案:

答案 0 :(得分:0)

我相信VB.NET中的Namespace语句会创建一个与项目的根命名空间相关的命名空间名称。尝试命名空间Controls而不是命名空间ASPNetWebApplication.Controls和/或使用ILSpy或类似方法检查生成的dll以查看已生成的命名空间。