빈 사용자 정의 컨트롤 (하위)이 있고 Page_Load 중에 일부 텍스트 상자와 버튼을 만듭니다. 또한 버튼에 이벤트를 추가합니다.핸들 단추 동적으로로드 된 사용자 정의 컨트롤에서 이벤트를 클릭하십시오.
그런 다음 다른 usercontrol (부모)의 자리 표시 자에서 해당 사용자 정의 컨트롤을 동적으로로드합니다. 두 컨트롤 모두 올바르게 렌더링되지만 버튼을 클릭하면 이벤트가 실행되지 않습니다. 이벤트 처리 방법에 대한 아이디어가 있으십니까?
아이 코드 :
protected void Page_Load(object sender, EventArgs e)
{
/*... other user controls ..*/
UpdateButton = new LinkButton();
UpdateButton.ID = "buttonid";
UpdateButton.Text = "text";
UpdateButton.Click += new EventHandler(UpdateButton_Click);
this.Controls.Add(UpdateButton);
}
protected override void Render(HtmlTextWriter writer)
{
for (int i = 0; i < this.Controls.Count; i++)
{
this.Controls[i].RenderControl(writer);
}
}
public void UpdateButton_Click(object sender, EventArgs e)
{
//Do stuff
}
부모 코드 :
protected void Page_Load(object sender, EventArgs e)
{
placeholder.Controls.Clear();
ChildControl uc = (ChildControl)LoadControl("~/UserControls/ChildControl.ascx");
placeholder.Controls.Add(uc);
}
내가 (부모없이 컨트롤을 예) 자식 컨트롤의 directy를 사용하는 경우 클릭 이벤트가 발생하고 친절하게 처리됩니다. 하지만 부모 컨트롤을 사용할 때 디버거는 UpdateButton_Click() 내에서 중단 점을 누르지 않습니다.
미리 감사드립니다.
부모와 자식을 사용할 때 자식의'Page_Load'가 제대로 실행됩니까? –
예. 자식 컨트롤 안에있는 모든 컨트롤 (텍스트 상자, 단추, 레이블)은 올바르게 렌더링되고 자식 컨트롤이 작동합니다. –