Home > DeveloperSection > Blogs > Difference between Server control and User control in asp.net

Difference between Server control and User control in asp.net


.NET ASP.Net 
Ratings:
0 Comment(s)
 913  View(s)
Rate this:

Difference between Server control and User control in asp.net

In this blog I will talk about server control and user control in ASP.NET

User Control

1.       1.User control is a page file with extension .ascx which can only be used within single application.

2.       User controls cannot  be added to the toolbox of  visual studio.NET. To use a user control with in an aspx page you have to drag the user control from the solution explorer to  designer Page.

3.       User Controls can be viewed as a sort of generic controls during the design time. The proper GUI of user controls can be viewed only during the run time.

Server control

1.       Custom controls are  assemblies (.dll files) that can be  used in multiple application.

2.       It can be added to toolbox of VS.NET.

3.       It can be viewed during the design time

Example:

User control:

Right click on solution Explorer>>Select Add option>>select New Item>>Web User control>>WebUserControl.ascx

 

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl.ascx.cs" Inherits="UserComtrolSample.WebUserControl" %>

<table align="center">

    <tr>

        <td colspan="2">

            <asp:Label ID="lblusername" runat="server" ForeColor="Red"></asp:Label>

        </td>

    </tr>

    <tr>

        <td>

           EmailID :

        </td>

        <td>

            <asp:TextBox ID="txtusername" runat="server"></asp:TextBox><asp:RequiredFieldValidator

                ID="RequiredFieldValidator1" runat="server"

                ControlToValidate="txtusername">*</asp:RequiredFieldValidator>

            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtusername" runat="server" ErrorMessage="*" ForeColor="#339966" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

        </td>

    </tr>

    <tr>

        <td>

            &nbsp;

        </td>

        <td>

            <asp:Button ID="btnSaveName" runat="server" Text="Save"

             style="height: 26px" />

        </td>

    </tr>

</table>

 

 

Create a Default.aspx file

Drag a drop webusercontrol.ascx on Default.aspx file

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

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

        <uc1:WebUserControl runat="server" ID="WebUserControl" />

    </form>

</body>

</html>

Example for Server control

Right click on solution Explorer>>Select Add option>>select New Item>>server control>>ServerControl.ascx

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace CustomEmailControl.Controls

{

public class RequiredTextBox : TextBox

    {

        private RequiredFieldValidator _required;

        private RegularExpressionValidator reqexp;

        public string InvalidMessage;

        public string RexInvalidMessage;

        public string ClientScript = "true";

        protected override void OnInit(EventArgs e)

        {

            _required = new RequiredFieldValidator();

            _required.ControlToValidate = this.ID;

            _required.ErrorMessage = this.InvalidMessage;

            _required.EnableClientScript = (this.ClientScript.ToLower() != "false");

            Controls.Add(_required);

        }

        protected override void Render(HtmlTextWriter w)

        {

             base.Render(w);

            _required.RenderControl(w);

        }

    }

}

 

Built solution explorer

Create a Default.aspx>>select toolbox>> add customControl.dll

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CustomEmailControl.Default" %>

<%@ Register Assembly="CustomEmailControl" Namespace="CustomEmailControl.Controls" TagPrefix="cc2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

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

    <div>

        <cc2:RequiredTextBox ID="RequiredTextBox1" InvalidMessage="Name is required."

                         ClientScript="false" runat="server" RexInvalidMessage="Invalid Expression"></cc2:RequiredTextBox>

        <asp:Button ID="Button1" runat="server" Text="Button" />

    </div>

    </form>

</body>

</html>

Output:


Don't want to miss updates? Please click the below button!

Follow MindStick