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

In order to familiarize myself with SharePoint development, I have created a custom web part to replace the default New, Edit, and Display forms of a custom list. Is there a way to use the already present SaveButton? This is what I have in SharePoint Designer.

<SharePoint:SaveButton runat="server" ControlMode="New" id="savebutton4"/>

I know there must be a way to connect this with my web part in Visual Studio 2010 on my server (maybe to my user control). If it helps, currently if I hit the save button, a blank item is created. So the button must be connected to something.

This project has been slow moving ever since I started it. In addition, if anyone has any good tutorial websites (or books) they use, please let me know.

Thanks.

EDIT

I've shelved trying to use the provided submit buttons and have started creating my own in my user control. So far, there have been far less headaches. I'll continue tomorrow and update if that works.

Working Custom Save/Submit Button

    protected void Page_Load(object sender, EventArgs e)
    {
        SPSite site = SPContext.Current.Site;
        SPWeb web = SPContext.Current.Web;
        SPList list = web.Lists["Contacts"];

        if (!IsPostBack) //This is why I couldn't edit the data
        {
            if (SPContext.Current.ListItem != null)
            {
                SPListItem listItem = SPContext.Current.ListItem;

                if (listItem["Last Name"] != null && listItem.Name != null)  //There must be data in Last Name and in the Title
                {
                    TextLastName.Text = listItem["Last Name"].ToString();    //If The data is null, then an exception occurs
                }
            }
        }
    }
    protected void submitbutton1_Click(object sender, EventArgs e)
    {
        SPSite site = SPContext.Current.Site;
        SPWeb web = SPContext.Current.Web;
        SPList list = web.Lists["Contacts"];

        SPListItem listItem = SPContext.Current.ListItem;
        SPListItem item = list.AddItem();

        if (listItem == null)               //Adds New form data
        {
            item["Last Name"] = TextLastName.Text;  
            item.Update();
        }
        else                                //Edits form data
        {
            listItem["Last Name"] = TextLastName.Text;
            listItem.Update();
        }

        //Used to close out of the modal window
        Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
        Context.Response.Flush();
        Context.Response.End();
    }

I'd still like any other suggestions for online tutorials or any good books on Sharepoint/ASP.NET.

Thank you for pointing me in the right direction Aaron.

share|improve this question
add comment

1 Answer

I would suggest just creating a new Button control and set its onclick handler to a function with something similar to the following:

SPWeb web = SPContext.Current.Web;
SPList destinationList = web.Lists["my destination list name"];

SPListItem item = destinationList.AddItem();
item["column1"] = "My data for Column 1";
item["column2"] = "My data for Column 2";
item.Update();

For books, I would suggest Inside Microsoft SharePoint 2010.

Thanks, -@SharePointAP

share|improve this answer
add comment

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.