Introduction:

In this article we will learn how to use User Control in asp.net or why use user controls in asp.net or what is user controls in asp.net with example.

Description:

In software development, every developer or programmer wants to reusability of the code. In asp.net web form User Control provide reusability of code to the developer. For example we need to Login Form in many places in my application. In this situation we create a user control and use it everywhere, whenever we required.

Let’s see how to use user control:

In the previous article we created a User Control, and now we will try using it for the first time.  

Simply create a webfrom1.aspx and write the code inside webfrom1.aspx.

For now, let's just declare it within the page. Add the following line below the standard page declaration:

<%@ Register TagPrefix="UC" TagName="UserLoginControl" Src="_Login.ascx" %>

Make sure that the src value matches the path to your UserControl file. Now you may use the UserControl in your page, like any other control. For instance, like this:

<UC:UserLoginControl runat="server" ID="UserLoginControl" />

Full example of use user control in asp.net.

Webfrom1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="TutorialsAsp.Net.WebForm1" %>
 
<%@ Register TagPrefix="UC" TagName="UserLoginControl" Src="_Login.ascx" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Use User Controls</title>
    
</head>
<body>
    <form runat="server">
        <UC:UserLoginControl runat="server" ID="UserLoginControl" />
    </form>
</body>
</html>

Now run your webform.aspx and see output like below:


I hope it will help to you after reading it.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.