在父用户控件中访问子用户控件的属性控件、用户、属性

2023-09-09 21:10:17 作者:轉角遇到Love

我在另一个静态代码中包含了一个用户控件:

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

将以下指令放在父页面的 asp 代码中或用户控制:

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

<%@ 注册 src="Name_of_your_child_control.ascx"tagname="Name_of_your_child_control" tagprefix="uc1" %>

在父页面/控件的asp-code中使用以下标签:

use the following tag in the asp-code of the parent page/control:

<uc1:Name_of_your_child_control ID="Name_of_your_child_control1"runat="server"/>

.....但问题是...我无法访问包含在给定用户控件(父用户控件)中的用户控件的公共属性(子用户控件)...

..... But the issue is...i am not able to access the public properties of user control which got included(child user control) in given user control(parent user control)...

请帮忙:(

推荐答案

假设你的用户控件是这样的:

Say your usercontrol was this:

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

您的控制代码隐藏:

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";