次の方法で共有


Render the all the column of links list through BaseFieldControl.

you need to set the "ItemContext" and "RenderContext" of the BaseFieldControl. If you create a SPContext only onetime,  assign to BaseFieldControl's "ItemContext" and "RenderContext" properties. First time when you add a webPart it a page, you will get all the control are rendering perfectly and providing you the correct data. But if you add the second instance of the same WebPart, the data of Links and Notes column are not comming repeating by the last row of the list. If you debug in the Visual Studio, you will get the error message "Operation is not valid due to the current state of the object"

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace ApplicationPages
{
    public class ControlTesterWebPart : WebPart
    {
        private SPList linksList;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.GetListInstance();
            this.BuildTable();
        }

        private void BuildTable()
        {
            HtmlTable table = new HtmlTable();
            HtmlTableRow row = null;
            HtmlTableCell fieldNameCell = null;
            HtmlTableCell fieldValueCell = null;
            SPField field = null;
            BaseFieldControl fieldControl = null;
            LiteralControl fieldValueControl = null;

            foreach (SPListItem linkItem in this.linksList.Items)
            {
                foreach (string fieldName in this.linksList.DefaultView.ViewFields)
                {
                    // Makle a new row
                    row = new HtmlTableRow();

                    // Get the SPField instance
                    field = linkItem.Fields.GetField(fieldName);
                    // Field Name
                    fieldNameCell = new HtmlTableCell();
                    fieldNameCell.InnerHtml = string.Format("{0}:&nbsp&nbsp", field.Title);
                    row.Cells.Add(fieldNameCell);

                    // Field Value
                    fieldValueCell = new HtmlTableCell();
                    fieldControl = field.FieldRenderingControl;
                    fieldControl.ID = Guid.NewGuid().ToString();
                    fieldControl.FieldName = field.InternalName;
                    fieldControl.ListId = this.linksList.ID;
                    fieldControl.ItemId = linkItem.ID;
                    fieldControl.ControlMode = SPControlMode.Display;
                    SPSite site = new SPSite(SPContext.Current.Site.ID);
                    SPWeb web = site.OpenWeb();
                    SPContext Context = SPContext.GetContext(HttpContext.Current, linkItem.ID, this.linksList.ID, web);
                    fieldControl.ItemContext = Context;
                    fieldControl.RenderContext = Context;
                    fieldValueControl = new LiteralControl(fieldControl.GetDesignTimeHtml());
                    fieldValueCell.Controls.Add(fieldValueControl);
                    row.Cells.Add(fieldValueCell);
                    if (web != null)
                        web.Dispose();

                    // Add the row to the table
                    table.Rows.Add(row);
                }

Comments