在 aspx 页面中查找 属性 个子子用户控件

Find property of sub child usercontrol in aspx page

有一个用户控件 - ChildUC.ascx 和 属性 "FirstName"

ChildUC.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ChildUC.ascx.cs" Inherits="SampleDevExpress14._2.UserControls.ChildUC" %>
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>
<asp:Button runat="server" ID="btnClick" Text="Click" OnClick="btnClick_Click"/>

ChildUC.ascx.cs - 这得到了 属性 FirstName

public partial class ChildUC : System.Web.UI.UserControl
    {
        public string FirstName { get; set; }
    }

这个ChildUC.ascx用户控件在另一个用户控件中使用ParentUC.ascx-

ParentUC.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ParentUC.ascx.cs" Inherits="SampleDevExpress14._2.UserControls.ParentUC" %>
<%@ Register src="ChildUC.ascx" tagPrefix="uc1" tagName="ChildUC" %>

<uc1:ChildUC runat="server" ID="ucChildUC"/>

ParentUC.ascx中使用了ParentPage.aspx

ParentPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ParentPage.aspx.cs" Inherits="SampleDevExpress14._2.UserControls.ParentPage" %>
<%@Register src="ParentUC.ascx" tagPrefix="uc1" tagName="ParentUC" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:ParentUC runat="server" ID="ucParentUC"/>    
    </div>
    </form>
</body>
</html>

ParentPage.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            UserControl ucChild = (UserControl)ucParentUC.FindControl("ucChildUC");
            TextBox txtNameOfChildUC = (TextBox)ucChild.FindControl("txtName");
            txtNameOfChildUC.Text = "From Parent Page";
        }

我能够在 ParentPage.aspx 中找到 ChildUC.ascxtextbox 控件 我在 Page_Load 事件

中显示

但是如何在[中找到ChildUC.ascxFirstName属性ParentPage.aspx 并为其设置一个值。

谢谢。

尝试使用动态,

    dynamic u = (UserControl)ParentControl.FindControl("childcontrol");
    u.firstname = "fawad";
    Response.Write(u.firstname);