Access child user control's property in parent user control

后端 未结 4 1082
悲&欢浪女
悲&欢浪女 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:01

    Using

    Name_of_your_child_control1.PublicPropertyName
    

    must work for your parent user control.

    0 讨论(0)
  • 2021-01-15 02:06

    Say your usercontrol was this:

    <%@ Control Inherits="Project.MyControl" Codebehind="MyControl.ascx.cs" %>
    <asp:TextBox ID="TB" runat="server" />
    

    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" %>
    <uc1:MyControl ID="MyControlID" runat="server" />
    

    You can use that property in code:

    MyControlID.MyTextProperty = "bob";
    
    0 讨论(0)
  • 2021-01-15 02:16

    Check the path and file names you are using, Anish. You have something wrong. Is Visual Studio telling you it can't find the control? Is it failing at compile time? Runtime?

    0 讨论(0)
  • 2021-01-15 02:20

    It's funny but whenever you add a property to a user control.

    You need to register it again in the parent. So in your case,

    Add a space at the end of this line and remove it again: $<% Register src="~/MyControl.ascx" tagname="MyControl" tagprefix="uc1" %>

    This will re - register the user control and you will be able to access new properties.

    0 讨论(0)
提交回复
热议问题