{"id":993,"date":"2012-08-07T22:14:12","date_gmt":"2012-08-07T21:14:12","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=993"},"modified":"2012-08-07T22:14:12","modified_gmt":"2012-08-07T21:14:12","slug":"peopleeditor_bound_control","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2012\/08\/07\/peopleeditor_bound_control\/","title":{"rendered":"PeopleEditor in Bound Control"},"content":{"rendered":"<p>Having problems updating a dynamically created PeopleEditor control on postback when utilising it in a bound repeating control?  The following solution might help you.<\/p>\n<p>In the CreateChildControls method of your control create and add a HiddenField control at the same level as the PeopleEditor control you want to update:<\/p>\n<pre lang=\"csharp\">\r\npeopleEditor = new PeopleEditor();\r\n\/\/ Initialise your PeopleEditor\r\n\/\/ ...\r\nControls.Add(peopleEditor);\r\n\r\n_hiddenField = new HiddenField { ID = \"_hiddenField\" };\r\nControls.Add(_hiddenField);\r\n<\/pre>\n<p>and then in the ItemCommand event (for example) of your bound control, set the value of the HiddenField control to the value of the PeopleEditor:<\/p>\n<pre lang=\"csharp\">\r\n_hiddenField.Value = peopleEditor.CommaSeparatedAccounts.Replace(\",\", \"; \");\r\n<\/pre>\n<p>Here we replace the , with a ; as this is the separator used within the page DOM by the people editor.<br \/>\n<!--more--><br \/>\nYou should also set the value of the hidden field to -1 after you&#8217;ve created it when it&#8217;s not a post back.  We&#8217;ll use this value to determine if the PeopleEditor should have it&#8217;s value updated by the JavaScript function located at the end of this post.<\/p>\n<pre lang=\"csharp\">\r\nif (Page.IsPostBack)\r\n    _hiddenField.Value = \"-1\"\r\n<\/pre>\n<p>For the JavaScript (using JQuery) add the following:<\/p>\n<pre lang=\"JavaScript\">\r\n$(document).ready(function () {\r\n\t$(\"[id$='_hiddenField']\").each(function() {\r\n\t\tupdatePeopleEditor($(this), 'peopleEditor');\r\n\t});\r\n});\r\n\r\nfunction updatePeopleEditor(sender, id) {\r\n\tvar peopleEditor = sender.parents('td').eq(0).find(\"[id$='\" + id + \"']\");\r\n\tvar upLevelDiv = peopleEditor.find('textarea').prev();\r\n\tvar checkNames = sender.parents('td').eq(0).find(\"[id$='\" + id + \"_checkNames']\");\r\n\tvar assignees = sender.val();\r\n\t\r\n\tif (assignees == undefined || assignees == null)\r\n\t\tassignees = '';\r\n        \r\n\tif (assignees != \"-1\") {\r\n                upLevelDiv.html(assignees);\r\n\t\tsetTimeout(function () { checkNames.click(); }, 100);\r\n        }\r\n}\r\n<\/pre>\n<p>This script calls the updatePeopleEditor function for each hidden field ending with the given id (_hiddenField), which in turn updates the value of the people editor with the value within the hidden field &#8211; values separated with ;<\/p>\n<p>The first thing this function does is locate the PeopleEditor control, which in this example is contained within the same table cell.<\/p>\n<p>Next, it locates the upLevelDiv that contains a hidden textarea. The people editor control uses this to store its values.<\/p>\n<p>After that, we find an anchor tag with an id that ends with _checkNames (part of the PeopleEditor markup).  We&#8217;ll use this to validate the values loaded into the textarea element.<\/p>\n<p>We now read the value of the hidden field and as long as the value is not -1 (which the control will have if the page hasn&#8217;t raised a post back), we update the upLevelDiv textarea and raise the click event of the anchor tag (checkNames) to validate the people editor control.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having problems updating a dynamically created PeopleEditor control on postback when utilising it in a bound repeating control? The following solution might help you. In the CreateChildControls method of your control create and add a HiddenField control at the same &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2012\/08\/07\/peopleeditor_bound_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":false,"jetpack_social_options":[]},"categories":[3],"tags":[85,51,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-g1","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/993"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=993"}],"version-history":[{"count":12,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/993\/revisions"}],"predecessor-version":[{"id":1124,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/993\/revisions\/1124"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}