2014-05-11 3 views
0

버튼에 새로운 텍스트 상자를 동적으로 추가하는 우수한 게시물 (jdavies로)을 발견했습니다 : 여기를 클릭하십시오 : https://stackoverflow.com/a/7677202/3620572.복합 컨트롤 : 알려진 요소가 아닙니다.

정확히이 작업을 수행하기 위해 C#을 사용하여 asp.net 프로젝트 내에서이 작업을 시도했지만 제대로 작동했습니다. 내가 C#을 읽는 데 문제가 있기 때문에 나는 vb.net으로 번역했다.

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 

제어입니다 나는 똑같이 배우려고 노력했다. 어떻게 작동하는지.

'요소'TextBoxCollection '입니다하지 알려진 요소입니다. "

및 default.aspx.designer에 오류 :

그러나 vb.net에서 나는의 default.aspx에 경고를 가지고있다

"형 'ASPNetWebApplication.Controls.TextBoxCollection는'정의 하지."

무엇 ㄴ 수 이 오류가 발생한 이유는 무엇입니까?

(이 질문은 Stackoverflow에 대한 첫 번째 질문이며 좋은 프로그래머는 아니지만 배우기를 원합니다. 따라서 정교하지 못한 질문과 내 영어도 사과하십시오. 팁.)

+0

나는 당신이 언급 한 게시물의 주석을 놓쳤다 고 생각합니다 : "참고 : 어셈블리 이름을"ASPNetWebApplication "을 어셈블리 이름으로 변경하십시오." –

+0

@Andrew Morton : 감사합니다. "ASPNetWebApplication"이라는 어셈블리의 이름을 지정하여 모든 것을 예제와 정확히 동일하게 만듭니다. 프로젝트 파일 : "ASPNetWebApplication.vbproj" 어셈블리 이름 : "ASPNetWebApplication" 루트 네임 스페이스 : "ASPNetWebApplication" – roland

+0

VB.NET의 네임 스페이스 문은 프로젝트의 루트 네임 스페이스와 관련된 네임 스페이스 이름을 만듭니다. 네임 스페이스 ASPNetWebApplication.Controls' 대신에'Namespace Controls'를 시도하고/또는 ILSpy를 사용하여 생성 된 dll을 검사하거나 유사하게 생성 된 네임 스페이스를 확인하십시오. – Joe

답변

0

VB.NET의 Namespace 문은 프로젝트의 루트 네임 스페이스와 관련된 네임 스페이스 이름을 만듭니다. 네임 스페이스 ASPNetWebApplication.Controls 대신 네임 스페이스 Controls을 시도하거나 ILSpy 또는 유사하게 생성 된 dll을 검사하여 네임 스페이스가 생성 된 것을 확인하십시오.

관련 문제