Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Sunday, 2 July 2017

Load User Control In Asp,Net WebForm

 asp.net, C#, user-control     No comments   


Introduction
In this article we will learn how to work with user-control In asp.net. How can we load the user-control in web form. What are the advantages of user-control and disadvantages. When do we need user control in our asp.net web application.

Previous Updates
In previous articles we have learnt Read Write Base64 Encoding And Decoding In SQL Server.  AngularJS vs JavaScript Example. Binding Data To HTML using AngularJS. CRUD operation using AngularJS.


What is User Control

A user control is self contained visual content that can be placed in the web page. User control is just like an Html control we can put it anywhere with in the web form. 

Why User Control
In the Software development field the most important thing is Reusability. In which way you write your code that used anywhere with in application if required with less modifications or without modification.
If we talking about the code then we used interfaces , inheritance , virtual methods and many concepts for reusability but when we think about Web Controls then how is it possible to use the same page controls with in different pages without writing the same code in every page.
It is possible using Web User Control or User Control.
Lets for example if your  application have different modules like Admin , User,  Super- Admin , Editor, Consumer etc and all have there login credentials. There may be any possibilities that any of them use the forget password functionality if required.  So all modules have there forget password pages with few different Words change and all controls probably the same.
In this case we will not use a WebForm to do this instead of we use a UserControl and call this Usercontrol in everypage where it required. It increases the reusability of your code.

Use UserControl In WebForm

Step 1 :  At first Add a new web form in your application.
Step 2 :  Add a UserControl in your Application. (If you did not find then search user control and select Web User Control and gave them a suitable name. find in below image).


Step 3 : After successfully added a user control Register it with in WebForm. You can register by simply drag and drop your user control in WebForm. Now your web form looks like 

<@ Register Src="~/UserControls/uc_MyUserControl.ascx" TagPrefix="uc1" TagName="uc_MyUserControl" >
<uc1:uc_MyUserControl runat="server" ID="uc_MyUserControl" />

Leave Register file on top and paste the uc1:uc_MyUserControl line where you want to display the UserControl.

Here is the Code sample of My Demo Web Form and User Control.

WebForm Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyWebForm1.aspx.cs" 
Inherits="UserControls_MyWebForm1" %>
<%@ Register Src="~/UserControls/uc_MyUserControl.ascx" TagPrefix="uc1" 
TagName="uc_MyUserControl" %>
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:Label ID="lblName" Text="This is Web Form " Font-Bold="true" 
runat="server"></asp:Label>
    </div>
     <div>
            <uc1:uc_MyUserControl runat="server" ID="uc_MyUserControl" />
     </div>
    </form>
</body>
</html>

UserControl Code 
<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="uc_MyUserControl.ascx.cs" Inherits="UserControls_uc_MyUserControl" %>
 <table>
     <thead> <h3> User Control Loaded </h3></thead>
   <tr>
      <td> <asp:Label ID="lblName" Text="First Name" runat="server"></asp:Label> </td>
      <td> <asp:TextBox ID="txtFName" Text="" runat="server"></asp:TextBox> </td>
   </tr>
   <tr>
     <td> <asp:Label ID="lblLName" Text="Last Name" runat="server"></asp:Label> </td>
     <td> <asp:TextBox ID="txtLName" Text="" runat="server"></asp:TextBox> </td>
    </tr>
</table>

OutPut


  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

comma seperated string in sql Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS row_number() and dense_rank() salary table structure. Scope_Identity() Use of indexes while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write