1
votes

In my form, I have 2 different ways of saving:

"Save" stores the user's input into the list through a postback, and allows further data entry after a jQueryUI popup dialog to confirm successful save.

"SaveClose" stores the user's input into the list through a postback, and then executes a contextual backout to return to the list page.

My problem lies in that if the user does a "Save" and then a "SaveClose", each save will create a new list item, resulting in the creation of 2 items.

In the code, it is obvious that this will occur:

        public void SaveClose_Click(object sender, EventArgs e)
        {
            _ControlBinder.SaveToList(CurrentMode.Equals(FormMode.New), delegate(...) { ...}); Backout();
        }
        public void Save_Click(object sender, EventArgs e)
        {
            _ControlBinder.SaveToList(CurrentMode.Equals(FormMode.New), delegate(...) { ... });
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "$(\"#savedDialog\").dialog(open);", true);
        }

When simply clicking "Save", the form is saved to the list, but the FormMode does not change. I have found the ChangeMode functionality, but have had no luck getting it to work.

Is there a way to change the mode of a form through C# codebehind? (Or a better conditional I could use besides CurrentMode.Equals(FormMode.New))

1

1 Answers

0
votes

Constructed a better conditional to first check the list where duplicates were being made, and ensure that another document with the same title did not exist before SaveToList was given a true.

The new condition is:

(CurrentMode.Equals(FormMode.New) && found.Count == 0)

Where found is created through the LINQ statement:

        SPList list = SPContext.Current.Web.Lists["ListName"];
        SPQuery query = new SPQuery();
        query.Query = @"
            <Where>
                <Eq>
                <FieldRef Name='Title' />
                <Value Type='Text'>" + Variable.ToString() + "</Value></Eq></Where>";
        SPListItemCollection found = list.GetItems(query);

I will continue to look for a solution which instead simply changes the FormMode to 'Edit' instead of 'New', but the LINQ solution ensures that no duplicates are created.