2011-09-12 2 views
1

템플릿이있는 두 개의 템플릿이있는 사용자 정의 컨트롤이 있으며, 존재하는 컨트롤에 대해 AssociatedControlID 또는 ControlToValidate와 같은 ID를 설정하는 것을 제외하면 모든 것이 작동합니다 다른 템플릿에서.두 개의 템플릿이있는 템플릿 사용자 컨트롤 컨트롤 ID가 필요한 속성의 다른 템플릿에서 컨트롤을 찾는 방법

두 개의 서로 다른 명명 컨테이너에서 서로를 찾을 수 없으므로 아래 코드에서 AssociatedControlID가 컨트롤 ID로만 작동하지 않는다는 것을 알고 있습니다. 이 선언적으로 설정하기위한 트릭이 있습니까? 아니면 FindControl을 사용하여 코드 뒤에 설정해야합니까?

<uc1:Field ID="paramName" runat="server"> 
    <LabelTemplate > 
     <asp:Label ID="lblName" AssociatedControlID="txtValue" runat="server" >Label Text:</asp:Label> 
    </LabelTemplate> 
    <InputTemplate> 
     <asp:TextBox ID="txtValue" runat="server" MaxLength="30"></asp:TextBox> 
    </InputTemplate> 
</uc1:Field> 

편집 : 1 : 나는 심지어 뒤에 코드에서 설정 동작하지 않습니다

여기 내 컨트롤 레이아웃입니다. AssociatedControlID를 .ID, .ClientID, .UniqueID로 설정하려고했는데, 나는 항상 잊어 버렸습니다. 나는 또한 FindControl에서 컨트롤을 찾는 시도했다. 부모의 부모는 아니지만 대조군의 부모에게서 찾을 수 있습니다. 그것은 매우 이상합니다.

편집 2 : 나는 부분적인 해결책을 생각해 냈습니다. 그것은 AssociatedControlID에 대해 작동하지 않지만 ControlToValidate에서 실제로 더 중요하게 작동합니다. 해결 방법은 다음과 같습니다. 컨테이너 클래스에서 FindControl을 재정의하고 두 단계를 가리켜 야합니다. 그런 다음 Parent.Parent.FindControl을 시도했지만 여전히 컨트롤을 찾지 못했기 때문에 자신의 FindControl을 수행해야합니다. 컨트롤 컬렉션의 모든 컨트롤을 열거 할 확장 메서드가 있으므로 완전히 이해하지 못합니다. 또한 레이블에 대해 AssociatedControlID와 함께 작동하지 않는 이유를 알 수 없습니다. 어쩌면 내부적으로 FindControl을 다르게 호출 할 수도 있습니다.

나는 아직도 내가 누락 된 것처럼 느낍니다. 여기

/// <summary> 
/// This is a hack to get the validators in the label template to see the controls in the input template 
/// just don't name any controls in the two templates the same thing 
/// </summary> 
/// <param name="id"></param> 
/// <returns></returns> 
public override Control FindControl(string id) 
{ 
    if (Parent == null && Parent.Parent == null) 
    { 
     return base.FindControl(id); 
    } 

    return Parent.Parent.Controls.All().Where(x => x.ID == id).FirstOrDefault(); 
} 

은 모든 확장 방법이 성가신 문제에 어떤 도움

public static IEnumerable<Control> All(this ControlCollection controls) 
{ 
    foreach (Control control in controls) 
    { 
     foreach (Control grandChild in control.Controls.All()) 
      yield return grandChild; 

     yield return control; 
    } 
} 

감사합니다. ControlToValidate를 들어

크리스

답변

0

, 당신은 InputTemplate 컨트롤의 클래스 정의에 ValidationPropertyAttribute를 사용할 수 있습니다. 간단하게 코드 숨김 업데이트로 : 예를 들면 : 당신은 사용자 정의 컨트롤의 ID로의 RequiredFieldValidator의 ControlToValidate 속성을 설정할 수 있습니다 뒤에 코드에서 이제

//"Text" is the name of the Property which returns the value to validate 
[ValidationProperty("Text")] 
public class InputTemplate 
{ 
    ... 
    public string Text { set { txtValue.Text = value; } get { return txtValue.Text } } 
} 

. RequiredFieldValidator1.ControlToValidate = InputTemplate1.ID;

Label의 AssociatedControlID에 대해서는 아직 설명하지 못했습니다.

최고의 행운의

,

관련 문제