Access Properties of a UserControl from other UserControl in ASP.Net Page

Few days ago I have posted an article "How To Access One UserControl from Another UserControl Using ASP.NET". When I try to find out solution for this I have got some nice articles about this issue. Rami vermula shows how to access properties of UserControl from another UserControl In asp.net.

This is really very helpful to solved my problem and immedidately me implement it in my solution.Now share for you .....

WebUserControl1.ascx
===================
This is the first UserControl on the page, from which we access the other UserControl (WebUserControl2.ascx) Properties.

Code
======

<%@ Control Language="C#" ClassName="WebUserControl1" %>
<!-- Add a Reference to WebUserControl2 using the @Reference Directive as shown -->
<%@ Reference VirtualPath="~/WebUserControl2.ascx"%>

<script runat="server">

// Create a Property to access the txtTransfer of WebUserControl1
public string WebUserControl1_txtTransfer
{
get
{
return txtTransfer.Text;
}
set
{
txtTransfer.Text = value;
}
}

// Handle the btnSubmit Click Event
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Access the WebUserControl2 control from the page.
WebUserControl2 oWUCtrl2 = (WebUserControl2)Page.FindControl("uc2");

// Set the TextBox Property of WebUserControl2 by accessing the public property of WebUserControl2
if(oWUCtrl2 != null)
oWUCtrl2.WebUserControl2_txtBalance = WebUserControl1_txtTransfer;
}

</script>

<asp:Panel ID="Panel1" runat="server" BorderColor="#000066" BorderStyle="Solid" Width="400px">

<asp:Label ID="Label1" runat="server" Font-Italic="True" Font-Size="Large"
Text="WebUserControl1" ForeColor="#000066"></asp:Label>
<br />;

Enter a Value To transfer to WebUserControl2 -
<asp:TextBox ID="txtTransfer" runat="server"></asp:TextBox>
<br />

<asp:Button ID="btnSubmit" runat="server" Text="Take the Value to WebUserControl2" OnClick="btnSubmit_Click" /><br />
</asp:Panel>


WebUserControl2.ascx
====================
This is the second UserControl on the page, we get data from to this UserControl from other UserControl (WebUserControl1.ascx) .

Code
======
<%@ Control Language="C#" ClassName="WebUserControl2" %>

<script runat="server">

/* Create a Property to set, get the value of the txtBalance, which is used in the other
UserControl i.e., WebUserControl1 to set the value.*/
public string WebUserControl2_txtBalance
{
get
{
return txtBalance.Text;
}
set
{
txtBalance.Text = value;
}
}

</script>

<asp:Panel ID="Panel1" runat="server" BorderColor="Red" BorderStyle="Solid" Width="400px">
<asp:Label ID="Label1" runat="server" Text="WebUserControl2" Font-Italic="True"
Font-Size="Large" ForeColor="Red"></asp:Label>
<br />
Display the value got from WebUserControl1 -
<asp:TextBox ID="txtBalance" runat="server"></asp:TextBox>
<br />;
</asp:Panel>


DefaltPage.aspx
=================
Now we have used those two UserControls WebUserControl1 and WebUserControl2 in this page.

Code
=======
<%@ Page Language="C#"%>
<!-- Add References to WebUserControl2, WebUserControl1 using the @Reference Directive as shown -->
<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="UserControl" TagName="uc1" %>
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="UserControl" TagName="uc2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
/* Here the UserControls are loaded dynamically, using Page.Loadcontrol() method
Give the ID's of the Dynamic UserControls, by which we can access them on the page. */
WebUserControl1 oWUCtrl1 = (WebUserControl1)Page.LoadControl("~/WebUserControl1.ascx");
WebUserControl2 oWUCtrl2 = (WebUserControl2)Page.LoadControl("~/WebUserControl2.ascx");
oWUCtrl1.ID = "uc1";
oWUCtrl2.ID = "uc2";

// Create a normal Html Tag control to get the breaks between dynamic Usercontrols
HtmlGenericControl hgc = new HtmlGenericControl();
hgc.InnerHtml = "<br/><br/>";

// Add the created dynamic control to the page
Page.Form.Controls.Add(oWUCtrl1);
Page.Form.Controls.Add(hgc);
Page.Form.Controls.Add(oWUCtrl2);
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>

Comments

Danny Morison said…
It's such a important site. cool, extraordinarily interesting!!!
Sandip sharma said…
it's cool!!!

Popular posts from this blog

The model backing the 'MyDBContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

How can I get a value of a property from an anonymous type (C#)

Check/Uncheck all items in a CheckBoxList using Javascript