2012-03-05 2 views
1

동적으로로드하는 사용자 정의 컨트롤에 기본 TextBox가 있습니다. 이 TextBox에 값을 전달하는 방법을 알아 냈습니다. 내가 겪고있는 문제는이 TextBox에서 입력 된 값을 가져올 수 없다는 것입니다.동적으로로드 된 사용자 정의 컨트롤에서 게시물 값에 액세스하는 방법

동적으로로드 된 컨트롤에 변수를 전달하는 클래스를 만들어야했습니다. PBUserControl이라고했습니다.

public class PBUserControl : UserControl 
{ 
    public IList<NVP> NameValuePairs { get { return _NameValuePairs; } } 

    public class NVP 
    { 
     public NVP() { } 
     public NVP(string name, string value) 
     { 
      this.Name = name; 
      this.Value = value; 
     } 
     public string Name { get; set; } 
     public string Value { get; set; } 
    } 
} 

이것은 사용자 정의 컨트롤로 데이터를 전송하는 데는 문제가없는 것 같습니다.

내 동적으로로드 된 사용자 컨트롤 :

public partial class PageAddEdit : PageBase 
{ 
    private PBUserControl _PBUserControl; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     // Must always desigante the control here else it dissapears. 
     Control TemplateUserControl = LoadControl("~/Administrator/PageBuilder/Templates/Basic.ascx"); 
     _PBUserControl = (PBUserControl)TemplateUserControl; 
     _PBUserControl.AddNameValuePair("TestContent", "It is working!!!!!!!!!"); 
     _PBUserControl.AddNameValuePair("Test", "Some test text."); 
     Template.Controls.Add(TemplateUserControl); 
    } 

    protected void MenuGroupRadMenu_Clicked(object sender, EventArgs e) 
    { 
     IList<PBUserControl.NVP> nvp = _PBUserControl.NameValuePairs; 
    } 
} 

나 여기 무슨 일이 생겼는지 설명 : 여기에

public partial class Basic : PBUserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (this.IsPostBack == false) 
     { 
      this.InitializeControl(); 
     } 
     else 
     { 
      this.ProcessSubmit(); 
     } 
    } 

    private void InitializeControl() 
    { 
     DynamicLiteral.Text = "Some Dynamic Content. " + GetNameValue("TestContent"); 
     TestTextBox.Text = GetNameValue("Test"); 
    } 
    private void ProcessSubmit() 
    { 
     AddNameValuePair("TestContent", "Passing back some value... "+ DynamicLiteral.Text); 
     AddNameValuePair("Test", TestTextBox.Text); 
    } 
} 

는이 동적으로로드 제어를 포함하는 페이지의 호출이다. 메인 페이지에서 PBUserControl의 값을 설정할 수 있습니다. 동적으로로드 된 사용자 정의 컨트롤로 완벽하게 전달됩니다. 그것이 무엇을 해야하는지 않습니다. 멋지게 작동합니다.

그러나 포스트 백 변수가 처리되기 전에 동적으로로드 된 컨트롤 내의 page_load 이벤트가 시작됩니다. 그래서 그 시점에서 TextBox의 내용은 아직 읽혀지지 않습니다. 나는 연구를 통해 이것을 발견했다. 또한 동적으로로드 된 컨트롤 내에 Pre_Rendered 이벤트를 넣으면 디버거로 코드를 단계별로 실행할 때 TextBox 값을 볼 수 있습니다. 또한, 디버거를 사용하면 TextBox가 Page_Load 이벤트 중에 읽히지 않는다는 것을 알 수 있습니다. 내 연구 결과는 다음과 같습니다. 1. Page_Init 이벤트는 입니다. Page_Load 이벤트는 입니다. 3. 페이지 양식 변수가 읽히고 처리됩니다 (어떤 이벤트인지 모르겠 음). 4. 사용자 이벤트는 다음과 같습니다. OnClick 5 Pre_Rendered 이벤트가 호출됩니다.

3 단계 이후와 4 단계 전 또는 도중에 호출 할 이벤트를 만들 수 있어야합니다. 그러면 TextBox 값을 캡처하여 PBUserControl.NVP 변수 내에 값을 설정하여 읽을 수 있도록 할 수 있습니다 메인 페이지. PBUserControl.NVP 변수에 수동으로 값을 설정하면 주 페이지에서 볼 수 있으며 잘 읽습니다.

미리 감사드립니다.

답변

0

나는 그것을 알아 낸 !!!! 기본적으로 이벤트를 생성하고 동적으로로드 된 UserControl에서 Main UserControl으로 전달했습니다. 아래는 내가 작동하도록 변경 한 사항입니다. 모든 사람들이 그것이 어떻게 작동하는지 따라갈 수 있도록 빠른 설명. 상속을 통해 몇 가지 요소를 만들 수있는 PBUserControl이라는 외부 래퍼가 있습니다. 뒤에있는 아이디어는 데이터를 앞뒤로 전달하고 분명히 이벤트를 전달할 수있게하는 것입니다. 하위 컨트롤을 동적으로로드 할 때 요소 나 이벤트를 쉽게 묶을 수는 없습니다. PBUserControl 클래스는이 작업을 허용하는 공통 본드를 만듭니다. 제 목적을 위해 백엔드에서 데이터 구조가 어떻게 설정되는지 이상적으로 작동하도록 설정되었습니다. 그래서 그것을 조정할 필요가있을 것입니다. 그러나 이것은 개념을 따르기 쉽다는 것을 증명해야합니다.

동적 컨트롤 내의 동작을 PBUserControl에있는 ControlSubmittedHandler에 등록 할 수 있습니다.

ControlSubmittedDelegate를 통해 ControlSubmittedHandler를 로컬 이벤트에 할당하여 상위 컨트롤에서 동적 컨트롤의 동작을 등록 할 수 있습니다. 그 시점에서 나는 그 사건을 촉발시킬 수 있습니다.

충분한 설명이 여기에 만들 수있는 코드 변경은 작동 있습니다 : 나는에 Page_Load 이벤트를 변경 내 동적으로로드 컨트롤 내에서

public delegate void ControlSubmittedDelegate(object sender, EventArgs e); 
public System.EventHandler ControlSubmittedHandler; 

:

내가 클래스 PBUserControl에 다음을 추가

내 동적으로로드 컨트롤 내에서
protected void Page_Load(object sender, EventArgs e) 
{ 
    InitializeControl(); 
    this.ControlSubmittedHandler = new EventHandler(ControlSubmittedAction); 
} 

나는 추가 :

내 메인 컨트롤 내에서
public void ControlSubmittedAction(object sender, EventArgs e) 
{ 
    ProcessSubmit(); 
} 

나는 추가 : 내 메인 컨트롤 내에서

private PBUserControl _PBUserControl; 
private event PBUserControl.ControlSubmittedDelegate myEvent; 

나는 변경 :

protected void MenuGroupRadMenu_Clicked(object sender, EventArgs e) 
{ 
    myEvent = new PBUserControl.ControlSubmittedDelegate(_PBUserControl.ControlSubmittedHandler); 
    if (myEvent != null) myEvent(this, e); 

    IList<PBUserControl.NVP> nvp = _PBUserControl.NameValuePairs; 
} 
+0

꽤 맵시를! 잘 했어 – Andrew

관련 문제