我让ascx假设A.ascx,我在OnInit()上写了一个委托,如下所示
btnUpdate.Click += delegate
{
if (MaxLength.HasValue && txtText.Text.Length >= MaxLength.Value)
{
lblError.Text = string.Format(Resources.ErrorMessage_FieldLength, MaxLength);
return;
}
if (Update != null) Update(this, EventArgs.Empty);
};现在我想在B.ascx上调用这个委托btn click
protected void btnHdnAnswer_Click(object sender, EventArgs e)
{
// How to call above delegate..
} 在这方面请帮帮我
发布于 2011-08-23 19:38:07
使你的委托成为一个合适的方法。
btnUpdate.Click += delegate { DoUpdate(); }
...
public void DoUpdate()
{
if (MaxLength.HasValue && txtText.Text.Length >= MaxLength.Value)
{
lblError.Text = string.Format(Resources.ErrorMessage_FieldLength, MaxLength);
return;
}
if (Update != null) Update(this, EventArgs.Empty);
}确保在代码隐藏中将控件的Id设置为为其生成成员:
<Project:A runat="server" ID="MyBControl"/>然后从B(父)控件调用它:
protected void btnHdnAnswer_Click(object sender, EventArgs e)
{
MyBControl.Update();
} https://stackoverflow.com/questions/7146313
复制相似问题