0

I've written a custom SPField which extends SPFieldText. This Field allows the admin to create a column with a cloze-text, where a user has to fill the missing words. The admin marks the positions for missing words with [], like

Hi [there], how [are] you.

In this example the words "there" and "are" are the correct answers for the missing words.

Then i created the FieldControl that parses the cloze-text and renders the textfields for every missing word.

ClozeFieldControl

private ArrayList answers = new ArrayList();

protected override void CreateChildControls()
{
    string pattern = Regex.Escape("[") + @"(.*?)\]";
    Match m = Regex.Match(clozeText, pattern);
    ....
    while (m.Success)
    {
        // display the text until the first missing word
        ....
        // then render a textfield
        answer = new TextField();
        answers.Add(answer);
        this.Controls.Add(answer);
        ....
    }
}

public override object Value
{
    get
    {
        EnsureChildControls();

        StringBuilder stringBuilder = new StringBuilder();

        foreach (TextField answer in answers)
        {
            stringBuilder.Append(answer.Text.Trim());
            stringBuilder.Append(SPFieldMultiColumnValue.Delimiter);
        }

        return stringBuilder.ToString();  // when debugging, this value is correct: "there;#are;#"
    }

    set
    {
        EnsureChildControls();

        string[] userAnswers = Regex.Split(value.ToString(), SPFieldMultiColumnValue.Delimiter);

        int currentAnswerIndex = 0;
        foreach (TextField answer in answers)
        {
            answer.Text = userAnswers[currentAnswerIndex];
            currentAnswerIndex++;
        }
    }
}

When i'm adding a new list-item, only the value of the last textfield is saved in my list-item, though the value of the overridden Value-method (get) is correct when i'm debugging my solution.

The value in my list-item after saving the new list-item is

;#are;#

instead of

there;#are;#

Do you have any ideas, why Sharepoint or ASP is doing this?

Thanks in advance for your hints.


Update1:

While debugging I just found out, that in my custom SPField the methods GetFieldValue(string value) and GetValidatedString(object value) are called 3 times with my example cloze text (2 TextFields).

  • At the first call, the value of both methods are correct: "there;#are;#"
  • At the second call, the value has the value of the first Textfield: "there"
  • And the last call, the parameter "value" has the value of the second Textfield: "are"

So there is a method which calls the save-function for each TextField, but I can't find the reason

1
  • Basically i need a custom SPFieldMultiColumn with dynamically created TextFields (based on the count of missing words in my cloze-text), whose values are saved in the list-item separated by a delimiter. Any ideas? Commented Oct 30, 2013 at 20:04

1 Answer 1

0

I solved this problem by replacing the TextField with a TextBox ....

Your Answer

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

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