4

I am trying to use the ListFieldIterator control in my web part to render a list form. I have a button event that suppose to update the current item from the ListFieldIterator control, after I click the button it saves a blank item with and ID only and rest of the fields are left blank.

Can some please tell me what I have done wrong here:

    private ListFieldIterator _listFieldIterator;

    public ListIteratorWebPart()
    {
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitializeControl();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        var personList = SPContext.Current.Web.Lists["PersonList"];
        var item = personList.AddItem();

        _listFieldIterator = new ListFieldIterator
        {
            ListId = personList.ID,
            ItemId = item.ID,
            ControlMode = SPControlMode.New,
            EnableViewState = true
        };
        _listFieldIterator.DataBind();
        Controls.Add(_listFieldIterator);

        var saveButton = new HtmlButton();
        saveButton.InnerText = "Save";
        saveButton.ServerClick += saveButton_ServerClick;

        Controls.Add(saveButton);
    }

    void saveButton_ServerClick(object sender, EventArgs e)
    {
        _listFieldIterator.Item.Update();
    }

2 Answers 2

2

Why not keep OOTB SaveButton control? It should already be there in your rendering template:

<SharePoint:SaveButton runat="server"/>

If you need to style this button, you can style it with CSS (default css class is ms-ButtonHeightWidth, but certainly you can override it or attach another class via js).

If you want to perform some additional logic before/after saving, I would recommend creating an Event Receiver for that.

Moreover, if you replace the standard button, you should also care about ribbon integration, because in fact the Ribbon "Save" button is integrated with the SaveButton.

No point in creating a separate button, really.

P.S. If you still want to know how to do it, start Reflector/dotPeek/whatever and explore Microsoft.SharePoint.WebControls.SaveButton in Microsoft.SharePoint.dll. It's only 600 lines long and does exactly what you need ;)

1
  • I am going to give it a try, and thanks for the answer! Sep 13, 2013 at 11:31
0
  1. Create a Button
  2. Create a dictionary like below:

    private Dictionary<string, object> EnumerateFieldControls() { Dictionary<string, object> fieldValues = new Dictionary<string, object>(); try { foreach (Control validator in _listFieldIterator.ItemContext.FormContext.FieldControlCollection) { if (validator is TextField) { TextField fieldControl = validator as TextField; if (fieldControl != null) fieldValues.Add(fieldControl.FieldName, fieldControl.Value); } else if (validator is BooleanField) { BooleanField fieldControl = validator as BooleanField; if (fieldControl != null) fieldValues.Add(fieldControl.FieldName, fieldControl.Value); } else if (validator is FileField) { FileField fieldControl = validator as FileField; fieldValues.Add(fieldControl.FieldName, fieldControl.Value); } else { BaseFieldControl fieldControl = validator as BaseFieldControl; if (fieldControl != null) fieldValues.Add(fieldControl.FieldName, fieldControl.Value); } }

        }
        catch (Exception ex)
        {
            this.LogToSPDiagnostic(ReusableForm_EXCEPTION_PREFIX + ex.Message);
            lblError.Text = ex.StackTrace;
            lblError.Visible = true;
        }
        return fieldValues;
    }
    
  3. SaveButton code below:

    protected void btnSave_Click(object sender, EventArgs e) { if (this.Page.IsValid) { SPList objList = SPContext.Current.Web.GetListFromWebPartPageUrl(_ListURL); SPListItem objItem = objList.AddItem(); foreach (var value in fieldValues) { objItem[value.Key] = value.Value; } objItem.Updae(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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