Wednesday, December 30, 2015

Pass Web Part Property Value to User Control



Introduction:
In this article I will describe that how to pass web part properties into user control.
To do so I will take base of my below article where I explained that how to create user control, load it using webpart and deploy using feature.
Detail Steps:-
Once you read my article http://sharepoint.infoyen.com/2012/04/11/create-and-deploy-user-control-in-sharepoint-using-feature/ then you will understand that how to create, deploy user control using feature.
To pass webpart property into webpart follow below steps:-
1. Create web part property for example “EmpoyeeName” in webpart class “GenericUserControlPicker” , as mentioned below:-
private string employeeName = string.Empty;

[WebBrowsable(), Personalizable(),
WebDisplayName("Employee Name"),
WebDescription("Employee Name")]
public string EmployeeName
{
 get
 {
  return employeeName;
 }
 set
 {
  employeeName = value;
 }
}

2. Create regular c# .net property in user control as “EmpoyeeName” in user control class “HelloWorld”, as mentioned below:-
public string EmployeeName {get;set;}

3. Pass webpart property “EmployeName” value to user control as mentioned below code:
// this is line number 51 in "GenericUserControlPicker" class
userControl = (UserControl)LoadControl(UserControlPath);
// Check usercontrol is loaded or not.
// Also check if Employee Name has been entered or not
if(userControl!= null && !String.IsNullEmpty(EmployeeName))
{
 userControl.EmployeeName = EmployeeName;
}

4. Display webpart property “EmployeName” in user control, as mentioned below code:
protected void Page_Load(object sender, EventArgs e)
{
 lblTest.Text = EmployeeName;
}

No comments:

Post a Comment