What is Custom Controls and What is User Controls in ASP.NET, C#: Part 2

Introduction

In 
previous article part we have seen that if we wanna use User Control in any of content page we must have to register it at the top as given below.

<%
@ Register TagPrefix="user" TagName="RandomImage"Src="~/RandomImages.ascx" %>

But this is very-very difficult if we have to use that User Control to entire website. Don't worry we have its alternative by using couple of lines in Web.config file. If we register User Control in Web.config file we don't have to user above code in each and every page. After registering in Web.config file we can use that User Control in entire website. Here is a Web.config file that I am using.

Web.config File Code

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  
http://go.microsoft.com/fwlink/?LinkId=169433
  
-->
<
configuration>
       <
system.web>
              <
compilation debug="true" strict="false" explicit="true"targetFramework="4.0"/>
       </
system.web>
  <system.web>
    <
pages>
      <
controls>
        <
add
          tagPrefix="user"
          tagName="RandomImage"
          src="~/usercontrols/RandomImages.ascx"/>
      </
controls>
    </
pages>
  </
system.web>
</configuration>

Remember that when we are using User Control through Web.config file then User Control should be placed in other separate folder, there must not be any content file that .aspx file.

Default.aspx File Code

<%
@ Page Language="VB" CodeFile="Default.aspx.vb"Inherits="_Default"%>
<!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>
    <user:RandomImage
        ID="RandomImage1"
        Runat="server" />
    </div>
    </form>
</
body>
</
html>

Note: Continue in next part.

Comments

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples