title: code behind in newform.Aspx?
body: <p>I have a list that must be filled with a bit of code behind (prepopulating some fields, and do a bit of work on the save button.)</p>
  
  <p>What is the best way to do that ?</p>
  
  <p>thx</p>
  
  <p><strong>Edit:</strong> I ended by creating a custom webpart on the default.aspx. In this web part I have a bunch of : </p>
  
  <pre><code>&lt;table border="0" cellspacing="0" width="100%"&gt;
  &lt;tr&gt;
      &lt;td width="190px" valign="top" class="ms-formlabel"&gt;
          &lt;h3 class="ms-standardheader"&gt;
              &lt;nobr&gt;Title&lt;span class="ms-formvalidation"&gt; *&lt;/span&gt;
              &lt;/nobr&gt;
          &lt;/h3&gt;
      &lt;/td&gt;
      &lt;td width="400px" valign="top" class="ms-formbody"&gt;
          &lt;SharePoint:FormField runat="server" id="fldTitle" ControlMode="New" FieldName="Title" ListId="{MyListID}" /&gt;
          &lt;SharePoint:FieldDescription runat="server" id="ff1description" FieldName="Title" ControlMode="New" ListId="{MyListID}"/&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>This is working, but I found this in a bit painfull because I have to read each form field in code behind :</p>
  
  <pre><code>    private void Set(SPListItem item, string fieldInternalName, object fieldValue)
      {
          var field = item.Fields.GetFieldByInternalName(fieldInternalName);
          item[fieldInternalName] = fieldValue;
      }
      protected void Btn_Ok_Click(object sender, EventArgs e)
      {
  
          SPWeb thisWeb = SPContext.Current.Web;
          SPList myList= thisWeb.Lists["mylist"];
          SPListItem newItem;
          newItem= myList.Items.Add();
  
          var router = thisWeb.EnsureUser(@"myuser");
  
          Set(newItem, "Title", fldTitle.Value);
          Set(newItem, "OtherField", fldOther.Value);
          Set(newItem, "AnotherField", GetFromBusinessLogic());
  
  
          SPUtility.Redirect(thisWeb.Url, SPRedirectFlags.Default, System.Web.HttpContext.Current);
          newItem.Update();
  
      }
  </code></pre>
  
  <p>Is there any way to wrap all of this in a custom form container ? Maybe a custom ListFormWebPart with inner templates and code-behind events ?</p>