vb.netwebusercontrol1.ascx<%@ Control Language="vb" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.vb" Inherits="aspdotnet.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>temp_.aspxUI<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %> <%@ Page Language="vb" AutoEventWireup="false" Codebehind="temp_.aspx.vb" Inherits="aspdotnet.temp_"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>temp_</title>
</HEAD> <body MS_POSITIONING="FlowLayout"> <form id="Form1" method="post" runat="server"> <P> <uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1></P> <P> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P> </form> </body> </HTML>temp_.aspx.vb Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim lab As New Label lab.Text = "抵制日货" lab.ID = "lab0" Dim uc As UserControl = CType(Page.FindControl("WebUserControl11"), UserControl) CType(uc.FindControl("PlaceHolder1"), PlaceHolder).Controls.Add(lab) End Sub
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>temp_.aspxUI<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="temp_.aspx.vb" Inherits="aspdotnet.temp_"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>temp_</title>
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<P>
<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1></P>
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
</form>
</body>
</HTML>temp_.aspx.vb
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim lab As New Label
lab.Text = "抵制日货"
lab.ID = "lab0"
Dim uc As UserControl = CType(Page.FindControl("WebUserControl11"), UserControl)
CType(uc.FindControl("PlaceHolder1"), PlaceHolder).Controls.Add(lab) End Sub
{
WebUserControl wuc=(WebUserControl)Page.FindControl ("WebUserControl1");
Label l=new Label ();
l.Text ="this is a test";
wuc.PlaceHolder1.Controls .Add (l);
}另外,还需改动的的地方把ASCX文件中的protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;中的protected 改为public,
//设置mm
PlaceHolder.Controls.Add(mm);