blog

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

Difference between Server control and User control in asp.net

priyanka kushwaha7773 20-Feb-2015

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

User Control

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

 

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

<tablealign="center">

    <tr>

        <tdcolspan="2">

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

        </td>

    </tr>

    <tr>

        <td>

           EmailID :

        </td>

        <td>

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

                ID="RequiredFieldValidator1"runat="server"

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

            <asp:RegularExpressionValidatorID="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:ButtonID="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

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

<headrunat="server">

    <title></title>

</head>

<body>

    <formid="form1"runat="server">

        <uc1:WebUserControlrunat="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

{

publicclassRequiredTextBox : TextBox

    {

        privateRequiredFieldValidator _required;

        privateRegularExpressionValidator reqexp;

        publicstring InvalidMessage;

        publicstring RexInvalidMessage;

        publicstring ClientScript = "true";

        protectedoverridevoid OnInit(EventArgs e)

        {

            _required = newRequiredFieldValidator();

            _required.ControlToValidate = this.ID;

            _required.ErrorMessage = this.InvalidMessage;

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

            Controls.Add(_required);

        }

        protectedoverridevoid Render(HtmlTextWriter w)

        {

             base.Render(w);

            _required.RenderControl(w);

        }

    }

}

Built solution explorer

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

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

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

<!DOCTYPEhtml>

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

<headrunat="server">

    <title></title>

</head>

<body>

    <formid="form1"runat="server">

    <div>

        <cc2:RequiredTextBoxID="RequiredTextBox1"InvalidMessage="Name is required."

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

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

    </div>

    </form>

</body>

</html>

Output:

Difference between Server control and User control in asp.net

Difference between Server control and User control in asp.net


Updated 20-Feb-2015

Leave Comment

Comments

Liked By