簡體   English   中英

從asp.net中的另一個用戶控件調用usercontrol中的方法

[英]To call a method in usercontrol from another user control in asp.net

在下面的代碼中,我有2個用戶控件sample.ascxsample1.ascx 。我在sample.ascx有一個按鈕,當我單擊它應該調用sample1.ascx的方法時。 請幫我這樣做。

Sample.ascx

<%@ Register TagPrefix="gmas" TagName="FieldCont" Src="~/Controls/Sample1.ascx" %>
<asp:Button id="Savebtn" runat="server" Text="Save" OnClick="Save_Click"  />

代碼隱藏:

protected void Save_Click(object sender, EventArgs e)
{
}

Sample1.ascx

public bool IsValid()
{
}
 protected void Save_Click(object sender, EventArgs e)
        {

         Sample1 ctrlB = new Sample1();

            ctrlB.IsValid();
      }

確保您的用戶控制代碼是Sample1,如下所述:

public partial class Sample1 : System.Web.UI.UserControl

你可以在按鈕點擊事件中調用IsValid方法,如下所述:

var control = new Sample1();
var isvalid = control.IsValid();

注意:

  • Sample1.ascx.cs中的 IsValid方法應該是公共的
  • “Sample1”的命名空間應與“Sample”相同。 如果兩者具有不同的命名空間,那么'Sample1'的命名空間應該通過using指令包含在'Sample'中

我認為它必須簡單,你可以嘗試使用代碼。

(new sample1()).<functionTocall>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM