다음과 같은 방법 사용해보십시오 :
당신의 UserControl에서 이벤트를 정의
public delegate void UserControl2Delegate(object sender, EventArgs e);
public partial class UserControl2 : System.Web.UI.UserControl
{
public event UserControl2Delegate UserControl2Event;
//Button click to invoke the event
protected void Button_Click(object sender, EventArgs e)
{
if (UserControl2Event != null)
{
UserControl2Event(this, new EventArgs());
}
}
}
가 Controls 컬렉션을 재귀 및 이벤트 처리기를 부착하여 페이지/마스터로드 방법에 UserControl을 찾기를
UserControl2 userControl2 = (UserControl2)FindControl(this, "UserControl2");
userControl2.UserControl2Event += new UserControl2Delegate(userControl2_UserControl2Event);
...
void userControl2_UserControl2Event(object sender, EventArgs e)
{
//Do something
}
...
private Control FindControl(Control parent, string id)
{
foreach (Control child in parent.Controls)
{
string childId = string.Empty;
if (child.ID != null)
{
childId = child.ID;
}
if (childId.ToLower() == id.ToLower())
{
return child;
}
else
{
if (child.HasControls())
{
Control response = FindControl(child, id);
if (response != null)
return response;
}
}
}
return null;
}
희망 추신.
시도해 보겠습니다. "delegate"와 "eventHandler"의 차이점은 무엇입니까? –
대리자는 본질적으로 메서드에 대한 포인터입니다. 대리자 선언은 가리킬 수있는 메서드 서명을 정의합니다. 이 경우 메서드는 이벤트 처리기이며 UserControl2에서 이벤트가 호출 될 때 발생합니다. 다음 기사에서 내가 설명 할 수있는 것보다 더 잘 설명 할 때 이벤트 + 대표자를 너무 자세하게 설명하지 않겠다 : [이벤트 및 대표자 단순화] (http://www.codeproject.com/KB/cs/events.aspx) – jdavies