【问题标题】:How to call ascx delegate to some other ascx btn click?如何调用 ascx 委托给其他一些 ascx btn 点击?
【发布时间】:2011-08-22 10:46:35
【问题描述】:

我有 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..  

    } 

请帮帮我

【问题讨论】:

  • 请问您为什么在 mvc 项目中使用 Web 表单服务器端逻辑?
  • 这看起来不像 MVC...
  • A 控件包含在 B 控件中,还是它们只是在同一个页面上?
  • @Tim Rogers:控件 A 像这样在控件 B 中,我正在使用控件 跨度>

标签: asp.net c#-4.0 delegates


【解决方案1】:

让你的委托成为一个合适的方法。

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();
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多