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.



