user1282609
user1282609

Reputation: 575

Find property of sub child usercontrol in aspx page

There is a User Control - ChildUC.ascx with Property "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 - This got a property FirstName

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

This ChildUC.ascx user control used in another user control 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"/>

The ParentUC.ascx used in a 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";
        }

I am able to find the textbox control of ChildUC.ascx in ParentPage.aspx which i showed above in Page_Load event

But how can I find the FirstName Property of ChildUC.ascx in ParentPage.aspx and set a value to it.

Thank you.

Upvotes: 0

Views: 956

Answers (1)

user786
user786

Reputation: 4374

Trying using dynamic,

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

Upvotes: 1

Related Questions