2012-06-19 3 views
1

ASP.NET에서 새 서버 컨트롤 형식을 만들려고합니다. 이 컨트롤은 RequiredFieldValidator를 특정 위치에 배치합니다. 내 클래스는 WebControl 클래스에서 상속되며 ControlID 속성은 주어진 컨트롤의 ID입니다. RequiredFieldValidator는 지정된 ControlID를 사용하여 컨트롤 근처에서 생성됩니다. 그러나 ControlID 값이 없습니다. 어떤 이벤트에서이 속성을 성공적으로 사용할 수 있습니까?왜 ASP.NET 서버 컨트롤 속성이 없습니다.

Public Class MyControl 
    Inherits WebControl 

    '... 
    Protected Property ControlID As String 
    Protected Property IsLinkedBrother As Boolean = False 
    '... 

    Protected Overrides Sub OnPreRender(e as System.EventArgs) 
     MyBase.OnPreRender(e) 
     Dim rootControl = If(IsLinkedBrother, Parent, Page) 
     Dim controls As Stack(Of Control) = New Stack(Of Control) 
     Dim currentControl As Control = Nothing 
     controls.Push(rootControl) 
     Dim result As Control = Nothing 
     While ((result Is Nothing) AndAlso (controls.Count > 0)) 
      currentControl = controls.Pop 
      If ((Not String.IsNullOrEmpty(currentControl.ID)) AndAlso (currentControl.ID.Equals(ControlID))) Then 
       result = currentControl 
      Else 
       For Each child As System.Web.UI.Control In currentControl.Controls 
        controls.Push(child) 
       Next 
      End If 
     End While 
     '... 
    End Sub 
'... 
End Class 

그러나 ControlID가 Nothing이고 이벤트가 예외를 throw합니다. ControlID는 초기화 후에 절대로 변경되지 않으며 다음과 같이 초기화됩니다.

<MyControl runat="server" ID="IDValue" ControlID="ControlIDValue" EnableCliendScript="true" 
    CssClass="Whatever" Display="Dynamic" /> 

나는 수 시간 동안 검색을했지만 운이 없었습니다. 아무도이 동작을 일으킬 수있는 이유와 그 이유를 말해 줄 수 있습니까? 누구에게도 해결책에 대한 제안이 있습니까? 사전에 감사합니다.

답변

2

ControlID는 공개되어 있어야 작동합니다. 보호 할 수 없으며 마크 업에서 설정할 수 없습니다.

+0

브라이언 감사합니다. 현재이 문제를 테스트하기 위해 프로젝트를 만들고 있습니다. 정말 고마워요. 나는 결과를 얻는대로 빨리 결과에 대해 쓸 것이다. –

관련 문제