ARTICLE

User control in ASP.NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET June 20, 2011
In this article we will see how to create user control and how to use it in our web forms.
 
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor