There are many scenarios that I always run into on projects or client sites that we need to create our own data entry forms for lists. Microsoft has really done a lot of work around this in exposing the objects that SharePoint uses so we can reuse these and have the same SharePoint look and feel. This post will show you how to build a data entry web part that uses the same objects that SharePoint does.
There are many discussion on creating web parts and rendering the controls that you create onto web parts. This post will not go into that, but will focus on the objects for a data entry form.
The control that we are dealing with is in the Microsoft.SharePoint.WebControls namespace. There are a couple things that you would want to determine before you actually create and render the control, like if the field is hidden or read only etc... After we determine if this field should be created and displayed we simply create a control called FormField. Once we create this control you simply set a couple properties and SharePoint will take care of the rest. The 3 main properties are ControlMode, ListId and FieldName. ControlMode tells the control which mode that this form is in (Display, Edit and New). The ListId tells the control which list on the site to hook to and FieldName tells the control which field that this control is attached to in the list. There is a fourth property to set if we are in Edit or Display mode and that is ItemId. If we set ItemId and are in Edit or Display mode, then SharePoint will automatically set the control value to that specific items value in the list. After we have added all of our fields, we can create a SaveButton control (In the same namespace, Microsoft.SharePoint.WebControls). You would also set the ControlMode and ListId properties on the save button to hook that specific button to the list as well. Once you have done all that, all you would need to do is add the controls created to your web parts collection (or however you render your controls inside a web part). Once you have the controls showing up, you can enter and edit data, click Save and SharePoint will take care of determining which list and item to hook to and actually updating the correct list item with the new values. There is also a property on the SaveButton called RedirectUrl. If you do not set this property, once the user hits Save it will automatically redirect the user back to the default list page. If you choose, you can set this property back to the calling page or a different page of your choice and when the user hits Save, they will be redirected back to that page. So you can build web parts that mimic the SharePoint data entry forms without the user ever having to go to the list itself. The other control we are using is the FieldLabel control. The FieldLabel control will render the actual label for that specific field. Below is the full code listing to render the web part.
// Create the table object that we are going to add the rows and cells to for our data entry form
Table oTable = new Table();
oTable.CellPadding = 0;
oTable.CellSpacing = 0;
// Get the site that this web part is running on.
SPWeb oWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context);
// Get the list we are going to work with
SPList oList = oWeb.Lists["Contacts"];
// Loop through the fields
foreach (SPField oField in oList.Fields)
{
// See if this field is not hidden
if (!oField.Hidden && !oField.ReadOnlyField && oField.Type != SPFieldType.Attachments)
{
// Create the label field
FieldLabel oLabelField = new FieldLabel();
oLabelField.ControlMode = SPControlMode.New;
oLabelField.ListId = oList.ID;
oLabelField.FieldName = oField.InternalName;
// Create the form field
FormField oFormField = new FormField();
oFormField.ControlMode = SPControlMode.New;
oFormField.ListId = oList.ID;
oFormField.FieldName = oField.InternalName;
// Add the table row
TableRow oRow = new TableRow();
oTable.Rows.Add(oRow);
// Add the cells
TableCell oCellLabel = new TableCell();
oRow.Cells.Add(oCellLabel);
TableCell oCellControl = new TableCell();
oRow.Cells.Add(oCellControl);
// Add the control to the table cells
oCellLabel.Controls.Add(oLabelField);
oCellControl.Controls.Add(oFormField);
// Set the css class of the cell for the SharePoint styles
oCellLabel.CssClass = "ms-formlabel";
oCellControl.CssClass = "ms-formbody";
}
}
// Create the save button
SaveButton oButtonSave = new SaveButton();
oButtonSave.ControlMode = SPControlMode.New;
oButtonSave.ListId = oList.ID;
// Create the row for the save button
TableRow oRowButton = new TableRow();
oTable.Rows.Add(oRowButton);
// Create the cell for the save button
TableCell oCellButton = new TableCell();
oCellButton.ColumnSpan = 2;
oRowButton.Cells.Add(oCellButton);
// Add the table to the web part controls collection
Controls.Add(oTable);
Following is a screen shot of the web part that was rendered with the above code.

Enjoy!!!