<asp:Label ID="Label1" Text="Mzwu.Com" runat="server" Height="20px" Width="226px"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Hello World!" Height="20px" Width="226px"></asp:Label>
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class WebUserControl : System.Web.UI.UserControl
{
public string Message
{
get { return Label1.Text; }
}
}
2.访问用控件里面的控件
Default.aspx:
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="MyControl" %>
<!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>访问用户控件中的控件-Mzwu.Com</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<MyControl:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(WebUserControl1.Message);//方法一:使用属性访问
Response.Write(((Label)WebUserControl1.FindControl("Label2")).Text);//使用FindControl方法
}
}