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.
http://sharepoint.infoyen.com/2012/04/11/create-and-deploy-user-control-in-sharepoint-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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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:-
1 | public string EmployeeName {get;set;} |
3. Pass webpart property “EmployeName” value to user control as mentioned below code:
1 2 3 4 5 6 7 8 | // 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:
1 2 3 4 | protected void Page_Load(object sender, EventArgs e) { lblTest.Text = EmployeeName; } |
Hope it helps!
Thanks!
Avinash
June 3, 2012
·
Infoyen ·
No Comments
Tags: MOSS, SharePoint, sharepoint web part development, user control, webpart · Posted in: MOSS, SharePoint, WebPart
Leave a Reply