访问用户控件的函数

📅 2026/7/5 11:15:30 👁️ 阅读次数 📝 编程学习
访问用户控件的函数
在asp.net网页中或者BasePage基类中,你有写上一个public方法,2个参数,传入字符类型和用户控件。
 public void XYZ(string myStr, UserControl myControlascx){//这里想访问用户控件的函数}

 

想在aspx.cs调用上面的方法,此方法内能调用到用户控件的函数。

简单的实现方法,写个interface,
public interface IFun
{void Fun1(string str); //方法string Fun2(string str); //返回函数
}

 

在你的用户控件中,实现这个接口,
2026-05-12_09-10-33

 

好吧,你那一个aspx网页想用到这个用户控件,拉进入即可。
2026-05-12_09-14-54


现在,我回到最开始提及的XYZ()方法,把它写完整,

 public void XYZ(string myStr, UserControl myControlascx){IFun obj = (IFun)myControlascx;obj.Fun1(myStr);   //把字符串传给方法。obj.Fun2(myStr);   //呼叫返回式方法。}

 

在你的aspx.cs中,去实现,

protected void Page_Load(object sender, EventArgs e)
{XYZ("Test Message", this.MyControl);
}

 

这样子,就可以调用到用户控件的函数了。
参考并试试....