{"id":1346,"date":"2013-02-08T22:27:23","date_gmt":"2013-02-08T22:27:23","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1346"},"modified":"2017-02-15T00:21:15","modified_gmt":"2017-02-15T00:21:15","slug":"sharing-properties-between-webpart-user-control","status":"publish","type":"post","link":"http:\/\/www.stuartroberts.net\/index.php\/2013\/02\/08\/sharing-properties-between-webpart-user-control\/","title":{"rendered":"Sharing WebPart and User Control Properties"},"content":{"rendered":"<p>When creating a Visual WebPart, sometimes you want the main functionality to be contained within the user control and not the WebPart, so that, for example, you can use the WebPart on multiple pages, one where the WebPart is hosted and users can edit the properties and another where only the user control is loaded and no WebPart manager, or WebPart zones, are available to allow editing of the properties.<\/p>\n<p>In the later case, the properties defined in the WebPart would either be set to default values or be retrieved from the query string.<\/p>\n<p>Now, how to setup the user control to receive the WebPart&#8217;s property values when contained in a WebPart?<br \/>\n<!--more--><\/p>\n<p>Take the following declaration within the WebPart class:<\/p>\n<pre lang=\"csharp\">\r\npublic class ExampleVisualWebPart : WebPart\r\n{\r\n\t\/\/ Other methods and properties excluded for brevity\r\n\t\r\n\t[Personalizable(PersonalizationScope.Shared), WebBrowsable(true)]\r\n\tpublic string ExampleProperty { get; set; }\r\n}\r\n<\/pre>\n<p>The first thing to do is to add a new public property to the user control using the WebPart as the object type:<\/p>\n<pre lang=\"csharp\">\r\npublic ExampleVisualWebPart ParentWebPartControl { get; set; }\r\n<\/pre>\n<p>and then create the same property declared in the WebPart for the user control:<\/p>\n<pre lang=\"csharp\">\r\nprivate string _customProperty;\r\n\r\npublic string ExampleProperty \r\n{\r\n\tget\r\n\t{\r\n\t\tif (ParentWebPartControl != null)\r\n\t\t\treturn ParentWebPartControl .CustomProperty;\r\n\r\n\t\treturn _customProperty;\r\n\t}\r\n\tset\r\n\t{\r\n\t\tif (ParentWebPartControl != null)\r\n\t\t\tParentWebPartControl .CustomProperty = value;\r\n\r\n\t\t_customProperty = value;\r\n\t}\r\n}\r\n<\/pre>\n<p>Lastly, back in the WebPart class, change the CreateChildControls method to:<\/p>\n<pre lang=\"csharp\">\r\nprotected override void CreateChildControls()\r\n{\r\n\tExampleVisualWebPartUserControl userControl = new ExampleVisualWebPartUserControl { ID = \"exampleVisualWebPartUserControl\", ParentWebPartControl = this };\r\n\tControls.Add(userControl);\r\n}\r\n<\/pre>\n<p>or<\/p>\n<pre lang=\"csharp\">\r\nprotected override void CreateChildControls()\r\n{\r\n\tExampleVisualWebPartUserControl userControl = Page.LoadControl(_ascxPath) as ExampleVisualWebPartUserControl;\r\n\tuserControl.ParentWebPartControl = this;\r\n\tControls.Add(userControl);\r\n}\r\n<\/pre>\n<p>This will load the user control and set the <em>ParentWebPartControl<\/em> to the current WebPart, allowing the user control to read the property values defined by the WebPart.  When there is no parent WebPart, the user control will simply read the local value contained within itself.<\/p>\n<p>You can easily extend this to read from a query string when this is the case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When creating a Visual WebPart, sometimes you want the main functionality to be contained within the user control and not the WebPart, so that, for example, you can use the WebPart on multiple pages, one where the WebPart is hosted &hellip; <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2013\/02\/08\/sharing-properties-between-webpart-user-control\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[3],"tags":[81,58,38],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-lI","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1346"}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1346"}],"version-history":[{"count":19,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1346\/revisions"}],"predecessor-version":[{"id":1810,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1346\/revisions\/1810"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1346"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}