开发者

How to call ascx delegate to some other ascx btn click?

开发者 https://www.devze.com 2023-03-29 23:50 出处:网络
I have ascx suppose A.ascx in which i am writing a delegate on OnInit() like this btnUpdate.Click += delegate

I have ascx suppose A.ascx in which i am writing a delegate on OnInit() like this

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

Now I want to call this delegate on B.ascx btn click

protected void btnHdnAnswer_Click(object sender, EventArgs e)
    {
       // How to call above delegate..  

    } 开发者_StackOverflow中文版

Please help me in this


Make your delegate a proper method.

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

Make sure the Id of your control is set to generate a member for it in the code-behind:

<Project:A runat="server" ID="MyBControl"/>

Then call it from your B (parent) control:

protected void btnHdnAnswer_Click(object sender, EventArgs e)
{
    MyBControl.Update();
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消