![](/img/trans.png)
[英]How can i call method from usercontrol from another usercontrol using asp.net?
[英]To call a method in usercontrol from another user control in asp.net
在下面的代碼中,我有2個用戶控件sample.ascx
和sample1.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();
注意:
using
指令包含在'Sample'中 我認為它必須簡單,你可以嘗試使用代碼。
(new sample1()).<functionTocall>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.