页面不能为null错误自定义MOSS 2007 Webpart

时间:2012-03-21 14:03:11

标签: asp.net sharepoint sharepoint-2007 moss

我正在尝试编写一个webpart,它基本上从列表中提取数据并将其显示给用户。有一个功能新闻列表,其中一列包含图像的URL。 Web部件应该获取3个最新的功能新闻项,并在页面上创建三个图像控件。单击按钮1,2,3可以滚动这些图像控件。 (即,单击2将设置图像1和图像3的Visible属性为false。)

我已经设法在这里实现了AJAX,但现在我正在尝试使用AJAX Control Toolkit中的UpdatePanelAnimationExtender。

我已经遵循了有关如何使用工具包,将其添加到GAC,添加安全程序集等的所有说明,并且已经超越了除此之外的所有错误:

“页面不能为空。请确保在ASP.NET请求的上下文中执行此操作”

我的完整代码如下:

    using System; using System.Collections.Generic; 
using System.Runtime.InteropServices; 
using System.Web.UI; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.WebControls;
 using System.Web.Extensions; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Utilities; 
using System.ComponentModel; 
using AjaxControlToolkit;

namespace FeatureNewsWebpartFeature {
    [Guid("7ad4b959-d494-4e85-b164-4e4231692b8b")]
    public class FeatureNewsWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _listName = null;
        private string _imageColumn = null;
        Image image1;
        Image image2;
        Image image3;
        UpdatePanelAnimationExtender upAnimator;


        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("List Connection Settings")]
        [WebDisplayName("List Name")]
        [WebDescription("Enter the name of the news list")]
        public string ListName
        {
            get
            {
                if (_listName == null)
                {
                    _listName = "News";
                }
                return _listName;
            }
            set { _listName = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("List Connection Settings")]
        [WebDisplayName("Image Column Name")]
        [WebDescription("Enter the column name of the ArtfulBits Image Upload column")]
        public string ImageUrlColumn
        {
            get
            {
                if (_imageColumn == null)
                {
                    _imageColumn = "Feature Image";
                }
                return _imageColumn;
            }
            set { _imageColumn = value; }
        }


        public FeatureNewsWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();



                    //Create script manager
                    if (ToolkitScriptManager.GetCurrent(this.Page) == null)
                    {

                        ToolkitScriptManager scriptHandler = new ToolkitScriptManager();
                        scriptHandler.ID = "scriptHandler";
                        scriptHandler.EnablePartialRendering = true;
                        this.Controls.Add(scriptHandler);
                    }


                    //Create update panel
                    System.Web.UI.UpdatePanel imageUpdatePanel = new System.Web.UI.UpdatePanel();
                    imageUpdatePanel.ID = "imageUpdatePanel";
                    imageUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
                    this.Controls.Add(new LiteralControl("<div id=\"updateContainer\">"));
                    this.Controls.Add(imageUpdatePanel);
                    this.Controls.Add(new LiteralControl("</div>"));




                    //Make SPSite object and retrieve the three most recent feature news items
                    SPSite site = SPContext.Current.Site;
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList oList = web.Lists[ListName];
                        SPQuery oQuery = new SPQuery();
                        oQuery.RowLimit = 3;
                        oQuery.Query = "<OrderBy>" +
                            "<FieldRef Name='Modified' Ascending='False' /></OrderBy>" +
                            "<Where>" +
                            "<Eq>" +
                            "<FieldRef Name='ContentType' />" +
                            "<Value Type='Choice'>Feature News Item</Value>" +
                            "</Eq>" +
                            "</Where>";


                        oQuery.ViewFields = string.Concat(
                            "<FieldRef Name='Feature_x0020_Image' />" +
                            "<FieldRef Name='Feature_x0020_Order' />");


                        image1 = new Image();
                        image2 = new Image();
                        image3 = new Image();

                        //For each item, extract image URL and assign to image object
                        SPListItemCollection items = oList.GetItems(oQuery);
                        foreach (SPListItem oListItem in items)
                        {
                            string url = oListItem["Feature_x0020_Image"].ToString();
                            url = url.Substring(url.IndexOf("/Lists"));
                            url = url.Remove(url.IndexOf(";#"));

                            switch (oListItem["Feature_x0020_Order"].ToString())
                            {
                                case "1":
                                    image1.ImageUrl = url;
                                    break;
                                case "2":
                                    image2.ImageUrl = url;
                                    break;
                                case "3":
                                    image3.ImageUrl = url;
                                    break;
                                default:
                                    //ERROR
                                    break;
                            }
                        }


                        if (!(Page.IsPostBack))
                        {
                            image1.Visible = true;
                            image2.Visible = false;
                            image3.Visible = false;
                        }


                        imageUpdatePanel.ContentTemplateContainer.Controls.Add(image1);
                        imageUpdatePanel.ContentTemplateContainer.Controls.Add(image2);
                        imageUpdatePanel.ContentTemplateContainer.Controls.Add(image3);


                        //Create animation for update panel
                        upAnimator = new UpdatePanelAnimationExtender();
                        upAnimator.ID = "upAnimator";
                        upAnimator.TargetControlID = imageUpdatePanel.ID;
                        const string xml = "<OnUpdating>" +
                            "<Parallel duration=\".25\" Fps=\"30\">" +
                            "<FadeOut AnimationTarget=\"updateContainer\" minimumOpacity=\".2\" />" +
                            "</OnUpdating>" +
                            "<OnUpdated>" +
                            "<FadeIn AnimationTarget=\"updateContainer\" minimumOpacity=\".2\" />" +
                            "</OnUpdated>";

                        Animation.Parse(xml, upAnimator);
                        this.Controls.Add(upAnimator);

                        Button b1 = new Button();
                        b1.ID = "b1i";
                        b1.Click += new EventHandler(b1_Click);
                        b1.Text = "Image 1";


                        Button b2 = new Button();
                        b2.ID = "b2i";
                        b2.Click += new EventHandler(b2_Click);
                        b2.Text = "Image 2";


                        Button b3 = new Button();
                        b3.ID = "b3i";
                        b3.Click += new EventHandler(b3_Click);
                        b3.Text = "Image 3";

                        this.Controls.Add(b1);
                        this.Controls.Add(b2);
                        this.Controls.Add(b3);

                        AsyncPostBackTrigger tr1 = new AsyncPostBackTrigger();
                        tr1.ControlID = b1.ID;
                        //tr1.EventName = "Click";
                        imageUpdatePanel.Triggers.Add(tr1);


                        AsyncPostBackTrigger tr2 = new AsyncPostBackTrigger();
                        tr2.ControlID = b2.ID;
                        //tr2.EventName = "Click";
                        imageUpdatePanel.Triggers.Add(tr2);


                        AsyncPostBackTrigger tr3 = new AsyncPostBackTrigger();
                        tr3.ControlID = b3.ID;
                        //tr3.EventName = "Click";
                        imageUpdatePanel.Triggers.Add(tr3);


                    }
                }
                catch (Exception ex)
                {

                    HandleException(ex);
                }
            }
        }

        void b1_Click(object sender, EventArgs e)
        {
            image1.Visible = true;
            image2.Visible = false;
            image3.Visible = false;
        }
        void b2_Click(object sender, EventArgs e)
        {
            image1.Visible = false;
            image2.Visible = true;
            image3.Visible = false;
        }
        void b3_Click(object sender, EventArgs e)
        {
            image1.Visible = false;
            image2.Visible = false;
            image3.Visible = true;
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

        }

        protected override void Render(HtmlTextWriter writer)
        {


            base.Render(writer);
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    } }

1 个答案:

答案 0 :(得分:0)

虽然在SP 2010的背景下询问了related question,但它仍然直接适用given the solution

我们的MOSS服务器场上有一个基本相同的CQWP:使用jQuery和SPServices插件从列表中读取项目并动画滑块更改。如果我没记错的话,这个过程中最困难的部分实际上是调整外观。一旦你有了正确的作品,把它放在一起就很容易了。