Ads

Friday, 15 July 2016

Create & add user control in master page

Lets create a user control which will display the login name of user.

Below steps needs to be performed.

Step-1:
VS --> empty Sharepoint Project --> Add user control (UControl)
Add below code in aspx page
<div id="Main" style="border:1; background-color:#2C84CA; font-size: 25px; color:White;">
    <asp:Label ID="lblUserName" runat="server" Text="0"></asp:Label>
</div>


In code behind file
 

Build and deploy the project. This will create new folder in 14 hive's Controltemplate's folder (In my case it was: \14\TEMPLATE\CONTROLTEMPLATES\MyUserControl\MyUserControl.ascx)


SharePoint 2010 add user control to page

1. Open SharePoint designer, Open the target site, and then Default.aspx in advanced mode.
2. Register the user control at the top of the page.
<%@ Register TagPrefix="MyUserControl"  TagName="UserName" Src="~/_controltemplates/MyUserControl/MyUserControl.ascx" %>

Insert the user control wherever required.

We've created user control for SharePoint 2010, Registered it on the page. Now, the final step is to  include the user control in the page, Here I've coded like:
 <MyUserControl:UserName id="MyUserControl1" runat="server" />



Result:

Refer Here





No comments:

Post a Comment

Ads