天天看點

使用者控件 之 通路使用者控件中的控件

1.使用者控件

webusercontrol.ascx

<%@ control language="c#" autoeventwireup="true" codefile="webusercontrol.ascx.cs" inherits="webusercontrol" %>

<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>

webusercontrol.ascx.cs

using system;

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:

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>

<%@ 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>

default.aspx.cs

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方法

繼續閱讀