title: SharePoint 2010 Web controls - LookupField value not being set on postback
body: <p>I've created a custom edit form as a simple aspx page in VS2010 (inherits from LayoutsPageBase) which uses the SharePoint Web controls LookupField control to display a drop down list of values from a custom type</p>
  
  <p>The form displays correctly with the drop down box containing the expected range of values</p>
  
  <p>The ControlMode is set to the same as the FormContext (though I have tried explicitly setting this to Edit)</p>
  
  <p>But on the postback the value of the dropdown list is not set - the selected item index is set to -1</p>
  
  <p>How can I correctly use the LookupField control to capture a selected value from the user?</p>
  
  <p>Could it be because I'm adding the controls declaritivly in the aspx and then setting the list id etc from the SPContext in the page load event? - see code snippet below (not the prettiest but just trying to get it to work at this point):</p>
  
  <p>from aspx:</p>
  
  <p></p>
  
  <pre><code>&lt;SharePoint:FileField ID="FileNameText"  InputFieldLabel="Name" runat="server" ControlMode="Display"/&gt;&lt;br /&gt;
  &lt;SharePoint:LookupField ID="FeedType" runat="server" /&gt;
  &lt;SharePoint:TextField ID="FeedStatus" runat="server"  /&gt;
  </code></pre>
  
  <p>....</p>
  
  <p>in the code behind page load:</p>
  
  <pre><code>if (!IsPostBack)
              {
                  SPItem feedFileItem = SPContext.Current.Item;
                  FileNameText.ControlMode = SPContext.Current.FormContext.FormMode;
                  FileNameText.ListId = SPContext.Current.ListId;
                  FileNameText.ItemId = SPContext.Current.ItemId;
                  FileNameText.FieldName = "Name";
  
                  FeedType.ControlMode = SPControlMode.Edit;
  
                  FeedType.ListId = SPContext.Current.ListId;
                  FeedType.ItemId = SPContext.Current.ItemId;
                  FeedType.FieldName = "FeedType";
  
                  FeedStatus.ItemContext = SPContext.Current;
                  FeedStatus.RenderContext = SPContext.Current;
                  FeedStatus.ControlMode = SPControlMode.Edit;
                  FeedStatus.ListId = SPContext.Current.ListId;
                  FeedStatus.ItemId = SPContext.Current.ItemId;
                  FeedStatus.FieldName = "FeedStatus";
              }
  </code></pre>
  
  <p>UPDATE</p>
  
  <p>Ok I managed to get my form working by adding the controls in the code behind in the override of CreateChildControls - this is in line with the majority of the samples I've seen on the net.</p>
  
  <p>But can someone explain why my approach didn't work and whether I can do this all in a declarative way in the aspx?</p>