ASP.NET(vb.net) & Web User Control and LoadControl() - How to use Web User Control and load control to Web Form (LoadControl()).
ShotDev Focus:
- ASP.NET(vb.net) & Web User Control - LoadControl()
Example 1
MyUserControl4_0.ascx (Web User Control)
<script Language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Me.lblText.Text = "Welcome To www.ShotDev.Com" End Sub </script> <table style="width: 500px" 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>
MyUserControl4_1.ascx (Web User Control)
<script Language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Me.lblText.Text = "Welcome To www.ShotDev.Com (1)" End Sub </script> <table style="width: 500px" 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>
MyUserControl4_2.ascx (Web User Control)
<script Language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Me.lblText.Text = "Welcome To www.ShotDev.Com (2)" End Sub </script> <table style="width: 500px" 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>
MyWebPage4.aspx (Web Form)
<%@ 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("~/MyUserControl4_1.ascx") ElseIF strPage = "page2" Then objCtrl = LoadControl("~/MyUserControl4_2.ascx") Else objCtrl = LoadControl("~/MyUserControl4_0.ascx") End IF Me.lblText.Controls.Add(objCtrl) End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</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>
Screenshot