用户控件UserControl.ascx
页面后台代码Default.aspx.cs
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControl.ascx.cs" Inherits="UserControl" %>
<asp:Label ID="Label1" runat="server" Text="LableText"></asp:Label>
用户控件后台UserControl.ascx.cs<asp:Label ID="Label1" runat="server" Text="LableText"></asp:Label>
public string zbx
{
get { return "UserControl"; }
}
public Label zbxLabel
{ get { return Label1; } }
页面Default.aspx{
get { return "UserControl"; }
}
public Label zbxLabel
{ get { return Label1; } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="UserControl.ascx" TagName="UserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div style="float:right;">
<uc1:UserControl ID="UserControl1" runat="server" visible="false"/>
</div>
</form>
</body>
</html>
<%@ Register Src="UserControl.ascx" TagName="UserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div style="float:right;">
<uc1:UserControl ID="UserControl1" runat="server" visible="false"/>
</div>
</form>
</body>
</html>
页面后台代码Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(UserControl1.zbx+"<br>");
Response.Write(((Label)UserControl1.FindControl("Label1")).Text+"<br>");
Response.Write(UserControl1.zbxLabel.Text);
}
{
Response.Write(UserControl1.zbx+"<br>");
Response.Write(((Label)UserControl1.FindControl("Label1")).Text+"<br>");
Response.Write(UserControl1.zbxLabel.Text);
}