2011-08-22 52 views
0

我的ascx想A.ascx在我寫這樣的如何調用ascx委託給其他ascx btn點擊?

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此委託)上的OnInit委託(BTN點擊

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

    } 

請幫助我在這

+1

對不起,但你爲什麼要在mvc項目中使用web表單服務器端邏輯? –

+0

這看起來不像MVC ... – Dismissile

+0

是一個包含在B控件中的控件,還是它們只在同一頁上? –

回答

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