Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I just created a custom new form web part in C#. It works great and adds a new item to the list with the 'Save' button.

Now, I am trying to reuse the same code to create the custom edit form. But I am not sure how the edit form will now already have all the data populated in the fields. Is there a way to bind the textboxes, etc to the list fields?

Also, now the save button should not add a new item to the list, rather it should edit the current item. What changes should be done in the save button onClick function?

The current ascx page is like -

<table cellpadding="10">

    <tr>
        <td>Created By:</td>
        <td><SharePoint:PeopleEditor ID="peoplePickerControl1" runat="server" Width="350px" height="4px"
                SelectionSet="User" /></td>
    </tr>

    <tr>
        <td>Created For:</td>
        <td><SharePoint:PeopleEditor ID="peoplePickerControl2" runat="server" Width="350" height="4px"
                SelectionSet="User" MultiSelect="False" /></td>
    </tr> 

    <tr>
        <td>Company: </td>
        <td><asp:TextBox ID="tbCompany" runat="server"></asp:TextBox>

        </td>
    </tr> 

    <tr>
        <td>Office: </td>
        <td><asp:TextBox ID="tbOffice" runat="server"></asp:TextBox>

        </td>
    </tr> 
....
</table>

And the save button onClick:

     protected void Button1_Click(object sender, EventArgs e)
        {

            SPListItemCollection listItems = web.Lists["ProblemTicket"].Items;
            SPListItem pt = listItems.Add();

            pt["CreatedBy"] = UserCollection1.ToString();
            pt["CreatedFor"] = UserCollection2.ToString();
            pt["Company"] = dpCompany.SelectedValue;
            pt["Office"] = dpOffice.SelectedIndex;
....
            web.AllowUnsafeUpdates = true;
            pt.Update();
            web.AllowUnsafeUpdates = false;

        }

Any help would be highly appreciated.

share|improve this question

1 Answer 1

I would suggest you to change the approach and to use SharePoint Field Controls. Unfortunately, I could not find a good article at the first glance, but the idea is the following.

You add a control on your aspx file (or from code-behind) and set several attributes of it:

<SharePoint:FormField ID="CompanyControl" runat="server" FieldName="Company" ControlMode="New"/>
<SharePoint:FormField ID="OfficeControl" runat="server" FieldName="Office" ControlMode="New"/>    
<SharePoint:SaveButton runat="server" ID="NewItemm" Text="Save"/>
<SharePoint:GoBackButton runat="server" ID="GoBackButtonn" />

In such an approach you can easily change type of the form just changing ControlMode attribute from "New" to "Display" or "Edit". The Save button does all what you need behind the scenes.

UPDATE1: If you want to connect your controls to another list (and item on disaply and edit forms), you could change its context using the following piece of code:

control.ListId = listId;               
SPContext context = SPContext.GetContext(cont, itemId, listId, web);
control.RenderContext = context;
control.ItemContext = context;
control.ItemId = itemId;
control.EnableViewState = true;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.