在父用户控件中访问子用户控件的属性 [英] Access child user control's property in parent user control

查看:37
本文介绍了在父用户控件中访问子用户控件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于在父用户控件中访问子用户控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆