2017-03-13 45 views
1

시각적 웹 파트와 두 개의 usercontrol이 있습니다. 비주얼 웹 부분 Page_Load()에서 동적으로 userControl1 만들기 : UserControl1에있어서다른 사용자 컨트롤에서 동적으로 생성 된 사용자 컨트롤에서 클릭 이벤트가 발생하지 않습니다.

protected void Page_Load(object sender, EventArgs e) 
{ 
    UserControl1 userControl = Page.LoadControl(userControl1Path) as UserControl1; 
    userControl.ID = "UserControl1"; 
    this.Controls.Clear(); 
    this.Controls.Add(userControl); 
} 

을 난 버튼로드하는 제 2 사용자 제어 (UserControl2를)가 (그리고 작동!) 또한

protected void GoToUserControl2_Click(object sender, EventArgs e) 
{ 
    UserContol2 userControl = Page.LoadControl(userControl2Path) as UserContol2; 
    userControl.ID = "UserContol2"; 
    this.Controls.Clear(); 
    this.Controls.Add(userControl); 
} 

UserControl2 버튼이 있지만 클릭 할 때 클릭 이벤트가 발생하지 않습니다. 대신 버튼을 클릭하면 UserControl1으로 리디렉션됩니다. 버튼에 아무런 이벤트가 없더라도 UserControl1으로 리디렉션됩니다.

도와주세요.

+0

하는 페이지로드에 브레이크 포인트를 넣어, 당신이를 Page_Load 함수가 다시 포스트가있을 때마다 실행됩니다 통지합니다, 나는 당신의 페이지로드는 컨트롤이 지워 의심 및 제어 1의 이벤트를 사용 –

답변

1

동적으로 생성 된 컨트롤은 모든 페이지로드시 다시 만들어야하며 포스트 백을 포함해야합니다. 두 번째 사용자 정의 컨트롤은 버튼 클릭에만로드되기 때문에 다른 포스트 백이 수행 될 때 사라집니다. UserContol2이 생성 된 경우 계속 추적해야하며, 그렇다면 부모의 Page_Load에 다시로드하십시오. 이 스 니펫에서는 UserContol2의 여는 내용을 추적하기 위해 Session을 사용하고 있습니다.

설정 버튼의 세션

protected void GoToUserControl2_Click(object sender, EventArgs e) 
{ 
    //rest of the code 
    Session["uc2_open"] = true; 
} 

방법

을 클릭하고 세션이 존재하는 경우를 Page_Load에 확인하고 만약 그렇다면, 두 번째 사용자 정의 컨트롤을 만듭니다.

protected void Page_Load(object sender, EventArgs e) 
{ 
    UserControl1 userControl = Page.LoadControl(userControl1Path) as UserControl1; 
    userControl.ID = "UserControl1"; 
    this.Controls.Clear(); 
    this.Controls.Add(userControl); 

    if (Session["uc2_open"] != null) 
    { 
     UserContol2 userControl = Page.LoadControl(userControl2Path) as UserContol2; 
     userControl.ID = "UserContol2"; 
     this.Controls.Clear(); 
     this.Controls.Add(userControl); 
    } 
}