SharePoint获取以编程方式创建的richtextbox控件的值

时间:2010-12-15 23:01:59

标签: sharepoint-2010 web-parts

我正在编写一个需要使用几个富文本框控件的自定义Web部件。我正在以编程方式将控件放到Web部件上。当Web部件获得保存回发时,我能够捕获除两个富文本框之外的所有字段中的数据。能够获得富文本框的价值的诀窍是什么?

我用来放置表单控件的代码是:

    private void CreateInputControls()
    {
        inputPanel.Controls.Clear();

        SPList list = SPContext.Current.Site.RootWeb.Lists["MyList"];
        SPContentType cType = list.ContentTypes[0];

        Table table = new Table();
        table.CellPadding = 3;
        table.CellSpacing = 0;

        SPContext newContext = SPContext.GetContext(System.Web.HttpContext.Current, list.DefaultView.ID, list.ID, list.ParentWeb);

        foreach (SPField field in cType.Fields)
        {
            if (!field.Hidden && field.CanBeDisplayedInEditForm)
            {
                FieldLabel fieldLabel = new FieldLabel();
                fieldLabel.ControlMode = SPControlMode.New;
                fieldLabel.ListId = list.ID;
                fieldLabel.FieldName = field.InternalName;
                fieldLabel.ItemContext = newContext;
                fieldLabel.RenderContext = newContext;
                fieldLabel.Field.Required = fieldLabel.Field.Required;

                FormField formField = new FormField();
                formField.ControlMode = SPControlMode.New;
                formField.ListId = list.ID;
                formField.FieldName = field.InternalName;
                formField.ItemContext = newContext;
                formField.RenderContext = newContext;
                formField.ID = field.InternalName;
                formField.EnableViewState = true;

                TableRow row = new TableRow();
                table.Rows.Add(row);

                TableCell cellLabel = new TableCell();
                TableCell cellField = new TableCell();

                cellLabel.Controls.Add(fieldLabel);
                cellField.Controls.Add(formField);
                row.Cells.Add(cellLabel);
                row.Cells.Add(cellField);
            }
        }

        inputPanel.Controls.Add(table);
    }

我用来保存新项目的代码是:

    private void UpdateItem(string bannerImageURL, string thumbnailImageURL)
    {
        SPList list = SPContext.Current.Site.RootWeb.Lists["MyList"];
        SPContentType cType = list.ContentTypes[0];
        SPItem item = list.AddItem();

        foreach (SPField field in cType.Fields)
        {
            if (!field.Hidden && field.CanBeDisplayedInEditForm)
            {
                FormField formField = (FormField)inputPanel.FindControl(field.InternalName);

                if (formField != null)
                {
                     // Saves data for all fields EXCEPT for rich text box (sharepoint multiline columns).
                     item[field.Title] = formField.Value; 
                }
            }
        }

        item.Update();
    }

2 个答案:

答案 0 :(得分:0)

可能是字段名称存在问题。尝试使用InternalName。

item[field.InternalName] = formField.Value;

答案 1 :(得分:0)

我一直在努力解决这个问题,我正在使用我认为发布的解决方法,因为这非常令人沮丧。

问题是RTE控件呈现为空,然后在客户端上使用JavaScript从隐藏控件填充。但是,这个隐藏的控件是可访问的服务器端:

switch (formField.Field.Type)
{
    case SPFieldType.Note:
        var rtf = (RichTextField)formField.Controls[0];
        item[field.Title] = rtf.HiddenInput.Value;
        break;
    default:
        item[field.Title] = formField.Value;
        break;
}

这可能需要扩展其他字段类型,但你明白了......