Friday, December 24, 2010

Public Property of User Control with Visual Web Part of SharePoint 2010

Problem:
I have tried to pass a string to a public property in User control which is registered in Visual Web Part but when i write UserControl1. , Visual Studio intellisense can't see this property so what i Have to do?

Solution:
For example ,In case you have a user control called "UserControl1" with public property called "Message" and Label Control to display this message in page load event as following :

UserControl1.ascx

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="SharePointProject1.ControlTemplates.SharePointProject1.UserControl1" %>
<asp:Label ID="lblMessage" runat="server" />

UserControl1.ascx.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SharePointProject1.ControlTemplates.SharePointProject1
{
    public partial class UserControl1 : UserControl
    {
        public string Message { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = Message;
        }
    }
}

Now I want to register this usercontrol in the Visual Web Part and Create a Property called "Message" which the user can enter the value by edit the web part in the sharePoint page then insert the string and after that save the web part.
Then we can pass this value to a property (Message) of UserControl as following :

VisualWebPart1UserControl.ascx
so here we have added and registered the User control in the Visual Web Part

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="SharePointProject1.VisualWebPart1.VisualWebPart1UserControl" %>
<%@ Register src="../UserControl1.ascx" tagname="UserControl1" tagprefix="uc1" %>

<uc1:UserControl1 ID="UserControl11" runat="server" />

VisualWebPart1.cs
so here we defined a property called a Message and we assigned the Current Webpart instacne object to the userControl  (control.currWebPart).

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SharePointProject1.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/SharePointProject1/VisualWebPart1/VisualWebPart1UserControl.ascx";

        protected override void CreateChildControls()
        {
            VisualWebPart1UserControl control = Page.LoadControl(_ascxPath) as VisualWebPart1UserControl;

            if (control != null)
            {
                control.currWebPart = this;
            }
            Controls.Add(control);
        }

        [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
        System.Web.UI.WebControls.WebParts.WebDisplayName("Message String"),
        System.Web.UI.WebControls.WebParts.Personalizable(),
        System.ComponentModel.Category("Settings")]
        public string Message { get; set; }
    }
}

VisualWebPart1UserControl.ascx.cs
Last we assigned the value of currWebPart.Message to the property of User control.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SharePointProject1.ControlTemplates.SharePointProject1;

namespace SharePointProject1.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        public VisualWebPart1 currWebPart { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            ((UserControl1)UserControl11).Message = this.currWebPart.Message;
        }
    }
}

Done.

7 comments:

Mostafa Absy said...

Thanks fadi, it is useful.

Ahmed Abbuabdou said...

True that it applies for Visual Web Part, but it's a generic casting issue to any other nested user control in a asp.net application. Casting is a MUST.

Fadi Ahmad Abdulwahab said...

I don't think so Mr.Ahmad because in normal asp.net project if you register a user control inside other user control you can access the public properties without casting but I think the problem come from SharePoint project template and not only this problem also you can't generate local resource from SharePoint template project
Regards,

Ahmed Abbuabdou said...

My bad

Unknown said...

Thanks!

Melih Özsoy said...

Thank you. Helped me much

Anonymous said...

http://www.ronaldwidha.net/2010/08/31/cant-access-public-properties-of-registered-user-controls-inside-a-sharepoint-2010-visual-web-part/