To access it from a web page you need to reference to the master page.
Here is how you do it.
Master Page
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="Dokimes_so_MasterPage" %>
<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Master Page Code Behind
public partial class Dokimes_so_MasterPage : System.Web.UI.MasterPage
{
// here we expose the control to public
public Dokimes_so_WebUserControl getCustomControl
{
get
{
return WebUserControl1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
One page base on this master page
please note this reference : <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
you need it so the page can now the class of the user control.
<%@ Page Title="" Language="C#" MasterPageFile="~/Dokimes/so/MasterPage.master" AutoEventWireup="true" CodeFile="GetControl.aspx.cs" Inherits="Dokimes_so_GetControl" %>
<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
and the code behind
public partial class Dokimes_so_GetControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// first you get the upper "master"
// then you get the custom control
// then you access a property inside the custom control
((Dokimes_so_MasterPage)Master).getCustomControl.cSetText = "test";
}
}
And the user control
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Dokimes_so_WebUserControl" %>
<asp:Literal runat="server" ID="txtTest"></asp:Literal>
and the code behind
public partial class Dokimes_so_WebUserControl : System.Web.UI.UserControl
{
public string cSetText
{
set
{
txtTest.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Some similar answers:
ASP.NET how to access public properties?