2014-05-11 55 views
0

我发现了一个很好的帖子(由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' 是没有定义 。”

什么可以b e这个错误的原因是什么? (这是我在Stackoverflow上的第一个问题,我不是一个好的程序员,但我想学习它。因此,请为我的简单问题(以及我的英文破碎问题)道歉。 )

+0

我怀疑你错过了你提到的帖子中的评论:“注意:将Assembly =”ASPNetWebApplication“更改为你的程序集的名称。” –

+0

@安德鲁莫顿:谢谢。我将程序集命名为“ASPNetWebApplication”,只是为了使所有内容与示例完全相同。 项目文件:“ASPNetWebApplication.vbproj” 程序集名称:“ASPNetWebApplication” 根名称空间:“ASPNetWebApplication” – roland

+0

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

回答

0

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

相关问题