ASP.NET LoadControl เป็น Method ไว้สำหรับโหลด Control ที่ได้จากการ Reference Control หรือ LoadControl มาจาก Class อื่น ๆ ในตัวอย่างผมได้ทดสอบการโหลด Control ของ Web UserControl
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim objCtrl As Control
Dim strPage As String
strPage = Request.QueryString("p")
IF strPage = "page1" Then
objCtrl = LoadControl("~/LoadControl_0.ascx")
ElseIF strPage = "page2" Then
objCtrl = LoadControl("~/LoadControl_1.ascx")
Else
objCtrl = LoadControl("~/LoadControl_2.ascx")
End IF
Me.lblText.Controls.Add(objCtrl)
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - LoadControl</title>
</head>
<body>
<form id="form1" runat="server">
<asp:HyperLink id="hplLink1" runat="server" NavigateUrl="?p=page1">Menu(1)</asp:HyperLink>
<asp:HyperLink id="hplLink2" runat="server" NavigateUrl="?p=page2">Menu(2)</asp:HyperLink>
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
LoadControl_0.ascx
<script runat="server" language="VB">
Sub Page_Load(sender As Object, e As EventArgs)
Me.lblText.Text = "Welcome To www.ThaiCreate.Com"
End Sub
</script>
<table style="width: 550px" border="1">
<tr>
<td style="height: 73px; text-align: center">
<asp:Label ID="lblText" runat="server" Font-Size="X-Large"></asp:Label></td>
</tr>
</table>
LoadControl_1.ascx
<script runat="server" language="VB">
Sub Page_Load(sender As Object, e As EventArgs)
Me.lblText.Text = "Welcome To www.ThaiCreate.Com (1)"
End Sub
</script>
<table style="width: 550px" border="1">
<tr>
<td style="height: 73px; text-align: center">
<asp:Label ID="lblText" runat="server" Font-Size="X-Large"></asp:Label></td>
</tr>
</table>
LoadControl_2.ascx
<script runat="server" language="VB">
Sub Page_Load(sender As Object, e As EventArgs)
Me.lblText.Text = "Welcome To www.ThaiCreate.Com (2)"
End Sub
</script>
<table style="width: 550px" border="1">
<tr>
<td style="height: 73px; text-align: center">
<asp:Label ID="lblText" runat="server" Font-Size="X-Large"></asp:Label></td>
</tr>
</table>