ASP.NET UserControl by VB.NET

In this article we will see how to create user control and how to use it in our web forms.
  • 2946

In this article, we will see how to create a user control for ASP.NET. A user control is a custom, reusable control in ASP.NET, saved in a file with an extension.ascx. you can reuse the Design as well as code in the application With the help of the user control. They cannot run on their own and they need to stand on another platform like an aspx page. Web user controls are derived from System.Web .UI.User Control namespace.

Step 1:

Create a new ASP .Net web application using Visual Studio .Net. 
 

uc1.gif
 

Figure1

Step 2:

Add a Web User Control to the project. Name it as UserControl1.ascx.

now select project and right click on it. 
 

uc2.gif
 

Figure2

Now select Add new Item.

uc3.gif
 

Figure3

Now click on the Add button.

Step 3:

Now add the following code with the webusercontrol1.ascx. Add two Textbox to the User Control as follows.

<div style ="border-left: 1px solid green; border-right: 1px solid green; border-bottom: 1px solid green; border-top: 20px solid green; width:309px;  ">

              <br />     

    login: <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>        

        <br />       

        password: <asp:TextBox ID="TextBox2"

            runat="server" Width="147px"></asp:TextBox>           

        <br />

        <br />

          login       

         </div>

Step 4:

Webpage1.aspx

Drag and drop the WebUserControl1.ascx into the Web form. This will put the following entries in the aspx page.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb"Inherits="webusercontrol.WebForm1" %>

 <%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>

<!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></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    </div>

    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />

    </form>

</body>

</html> 

Now build and run the application. 
 

uc4.gif
 

Figure4

Re-usability

As we have seen in this example, user controls can be built once and re-used any number of times as required in the same web page, web site or even across multiple web sites.

Conclusion:

In this article we have seen how to create user control and how to use it in our web forms.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.