如何从用户控件访问页面控件?有没有办法从用户控件访问页面控件。我的页面中有一些控件,我想从用户控件访问它们。YourControlTypeltMetaTags=null;控制ctl=this.Parent;while(true){ltMetaTags=(ControlType)ctl.FindControl("ControlName");如果(ltMetaTags==null){ctl=ctl.Parent;如果(ctl.Parent==null){返回;}继续;}休息;}示例System.Web.UI.WebControls.LiteralltMetaTags=null;控制ctl=this.Parent;while(true){ltMetaTags=(System.Web.UI.WebControls.Literal)ctl.FindControl("ltMetaTags");如果(ltMetaTags==null){如果(ctl.Parent==null){返回;}ctl=ctl.Parent;继续;}休息;实际上有几种方法可以做到这一点:创建一个公共属性publicButtonPageButton{get;放;}在用户控件中,然后在页面的OnInit或OnLoad方法中指定它myUserControl.PageButton=myPageButton;您可以公开控件并取消装箱页面:publicButtonPageButton{get{returnthis.myPageButton;在用户控件中:MyPagemyPage=(MyPage)this.Page;myPage.PageButton.Text="你好";最慢但最简单的方法是使用FindControl:this.Page.FindControl("myPageButton");它对我有用:我在我的.aspx页面中声明了Label在.aspx.cs中我通过面板添加了UserC控制UserControlobjControl=(UserControl)Page.LoadControl("~/ts1.ascx");pnlUC.Controls.Add(objControl);并从.ascxUserControl中访问,如下图:以上是C#学习教程:如何从用户控件中学习访问页面控件?如果分享的内容对你有用,需要了解更多C#学习教程,希望大家多多关注——Pagepage=this.Page;Labellbl=page.FindControl("lblpage")asLabel;stringtextval=lbl.Text;Parent.FindControl("hdnValue")本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
