Access child user control's property in parent user control

后端 未结 4 1100
悲&欢浪女
悲&欢浪女 2021-01-15 01:44

I have included a user control in another statically following code :


place the folowing directive in the asp code of the parent page or usercontrol:

<

4条回答
  •  深忆病人
    2021-01-15 02:06

    Say your usercontrol was this:

    <%@ Control Inherits="Project.MyControl" Codebehind="MyControl.ascx.cs" %>
    
    

    Your control code-behind:

    namespace Project 
    {
      public partial class MyControl : UserControl
      {
        public string MyTextProperty
        {
          get { return TB.Text; }
          set { TB.Text = value; }
        }
      }
    }
    

    In your parent page that included the control, like this:

    <%@ Register src="~/MyControl.ascx" tagname="MyControl" tagprefix="uc1" %>
    
    

    You can use that property in code:

    MyControlID.MyTextProperty = "bob";
    

提交回复
热议问题