The name defines itself: user control. The user can design his or her own control with a group of control. Afterwards we can useit anywhere in theapplication. We can use user control for displaying data and data entry forms.
A user control is stored in a separate file ASCX file extension.
User Control means collection and uses and combination of several ASP.NET controls. If your project requires any functionality reused on multiple pages, then code handling and maintenance become quite easy with user control.
User Control does not have <html>, <body> and <form> tag. User control is embedded within page.
User control is the concept of reusability; in shorta developing element that can be used in multiple places within a web application or web site.
User control has its own events, similar to a web page event.
Concept of user control is to create it one time and use n number of times withinthe application.
Step by Step creating a User Control
- Create a WebSite Project.
File-> New->WebSite.
Some examples or scenarios where we can use user control.
- In Leave web Application create a user control for displaying Leave balance.
- In E-commerce site or Shopping application create a user control for displaying Detailed Cart Information.
- In Club Member web application create a user control to display member.
- Create a new WebForm.
Right click on project Click ADD, then Add New Item.
- Create a new User Control.
Right click on project click ADD -> Add New Item.
- We had created a two file Default.aspx WEBFORM and FriendWebUserControl.ascx USER CONTROL.
- Double Click on DEFAULT.ASPX file.
Drag N Drop GridView Control from ToolBox.
- <asp:GridView ID="gvFriend" runat="server" AutoGenerateColumns="False">
- <Columns>
- <asp:BoundField DataField="FriendID" HeaderText="Friend ID" />
- <asp:BoundField DataField="FriendName" HeaderText="Friend Name" />
- <asp:ButtonField Text="View Detail" />
- </Columns>
- </asp:GridView>
I had added Three Fields:
- To display Friend ID
- To display Firend Name
- Button to View Friend Detail
In design view GRIDVIEW display like this.
To apply auto format style on GRIDVIEW, click on Black color marked button called SMART TAG.
I had selected BROWSUGAR category. After selection GRIDVIEW displays like this.
Till Now Your code behind file code of DEFAULT.ASPX:
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class _Default: System.Web.UI.Page
- {
- string ConStr = ConfigurationManager.ConnectionStrings["MemberCDACConnectionString"].ConnectionString;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindRepeater();
- }
- }
-
-
-
- public void BindRepeater()
- {
- SqlConnection con = new SqlConnection(ConStr);
- SqlDataAdapter da = new SqlDataAdapter("Select * From tblFriends", ConStr);
- DataSet ds = new DataSet();
- da.Fill(ds, "FriendsTable");
- gvFriend.DataSource = ds;
- gvFriend.DataBind();
- }
- }
- Now, double click FriendWebUserControl.ascx user control to work on.
- FriendWebUserControl.ascx file we can attach to any ASPX and utilize the full power of User Control.
In user control, we will create public property on that basis User Control will display particular friend details.
In User Control ASCX code, I had used a Repeater Control, Inside repeater control displayed Friend Details.
CODE OF FriendWebUserControl.ascx:
- <%@ Control Language="C#" AutoEventWireup="true" CodeFile="FriendWebUserControl.ascx.cs" Inherits="FriendWebUserControl" EnableViewState="True" %>
- <h3><u>Friend Detail From User Control</u></h3>
- <asp:Repeater ID="rptFriend" runat="server">
- <HeaderTemplate> </HeaderTemplate>
- <ItemTemplate>
- <table border="1" style="border-color:darkorange;background-color:lightgray">
- <tr>
- <td><b>Friend ID :</b>
- <asp:Label ID="lblFriendID" runat="server" Text='<%# Eval("FriendID") %>'></asp:Label>
- </td>
- </tr>
- <tr>
- <td><b>Friend Name :</b>
- <asp:Label ID="lblFriendName" runat="server" Text='<%# Eval("FriendName") %>'></asp:Label>
- </td>
- </tr>
- <tr>
- <td><b>Place :</b>
- <asp:Label ID="lblPlace" runat="server" Text='<%# Eval("Place") %>'></asp:Label>
- </td>
- </tr>
- <tr>
- <td><b>Mobile :</b>
- <asp:Label ID="lblMobile" runat="server" Text='<%# Eval("Mobile") %>'></asp:Label>
- </td>
- </tr>
- <tr>
- <td><b>Email ID :</b>
- <asp:Label ID="lblEmailID" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label>
- </td>
- </tr>
- </table>
- </ItemTemplate>
- <FooterTemplate> </FooterTemplate>
- </asp:Repeater> <br /> <br /> <a href="Default.aspx">Back To Main Page</a>
In User Control ASCX.CS code is used and created in the following ways:
- Created a FriendID property: Which receiving a value of Selected FriendID from GridView.
- Created a BindRepeater method: Which fetches data from Database and hands over to Repeater control.
CODE of FriendWebUserControl.ascx.cs:
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class FriendWebUserControl: System.Web.UI.UserControl
- {
- string ConStr = ConfigurationManager.ConnectionStrings["MemberCDACConnectionString"].ConnectionString;
- public int FriendID
- {
- get;
- set;
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- BindRepeater(FriendID);
- }
-
-
-
- public void BindRepeater(int SelectedFriendID)
- {
- SqlConnection con = new SqlConnection(ConStr);
- SqlDataAdapter da = new SqlDataAdapter("Select * From tblFriends where FriendID = " + SelectedFriendID, ConStr);
- DataSet ds = new DataSet();
- da.Fill(ds, "FriendsTable");
- rptFriend.DataSource = ds;
- rptFriend.DataBind();
- }
- }
- I will use usercontrol in another ASPX page.
- Right click on project and add new WebForm.
DisplayFriendDetail.aspx
- Double Click on DisplayFriendDetail.aspx page.
- How to use User Control?
The following are the ways:
a. Register the UserControl at the top the ASPX.
b. UserControl on the Page.
Otherwise:
Drag n Drop the User control on ASPX page.
- Run Application.
Output
Interview questions related to this article.
- What is a User Control?
- How to use User Control (Ascx) in web page.
- True / False : User control having <body>, <form>.
Read more articles on ASP.NET: