webpart中方法的正确顺序?

时间:2013-06-05 08:03:17

标签: c# sharepoint web-parts

在webpart开发中,有OnInit,CreateChildControls,OnPrerender等。

我有一个webpart,它应该添加一个带有一些属性,文本和url的链接按钮,具体取决于用户在属性工具箱上键入的内容

我不确定,我应该在哪个部分添加将链接按钮添加到页面的代码?设置属性等

这是我到目前为止所拥有的

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

namespace xxwC.SP.xx.WebParts.WebParts.LinkButton
{
    [ToolboxItemAttribute(false)]
    public class LinkButton : WebPart
    {
        System.Web.UI.WebControls.LinkButton LnkButton;
        #region Webpart properties
        [WebBrowsable(true), WebDisplayName("LinkText"), WebDescription("Text for the link"),
            Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
            System.ComponentModel.DefaultValue("")]
        public string LinkText
        { get; set; }

        [WebBrowsable(true), WebDisplayName("Link"), WebDescription("Link"),
            Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
            System.ComponentModel.DefaultValue("")]
        public Uri Link
        { get; set; }

        [WebBrowsable(true), WebDisplayName("OpenModal"), WebDescription("OpenModal"),
            Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
            System.ComponentModel.DefaultValue("")]
        public Boolean OpenModal
        { get; set; }

        [WebBrowsable(true), WebDisplayName("Width"), WebDescription("Width"),
            Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
            System.ComponentModel.DefaultValue("")]
        public int WidthPopup
        { get; set; }

        [WebBrowsable(true), WebDisplayName("Height"), WebDescription("Height"),
            Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
            System.ComponentModel.DefaultValue("")]
        public int HeightPopup
        { get; set; }

        [WebBrowsable(true), WebDisplayName("ClientCode"), WebDescription("ClientCode"), ReadOnly(true),
    Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
    System.ComponentModel.DefaultValue("")]
        public String ClientCode
        { get; set; }


        #endregion

        protected override void CreateChildControls()
        {
            this.Controls.Add(LnkButton);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);


            if (String.IsNullOrEmpty(ClientCode))
            {
                if (SPContext.Current.Web.AllProperties.ContainsKey("ClientCode"))
                {
                    ClientCode = SPContext.Current.Web.GetProperty("ClientCode").ToString();
                }
                else
                {
                    //TODO: Logging service - No Webproperty found
                }
            }

            RenderLinkButton();
        }

        private void RenderLinkButton()
        {
            if (LnkButton != null && Link != null && LinkText != null)
            {
                LnkButton.Text = LinkText;
                //Concat Link property with the QueryString ClientCode
                String fullLink = String.Format("{0}?ClientCode={1}", Link.ToString(), ClientCode);

                if (OpenModal)
                {
                    LnkButton.Attributes.Add("onclick", "OpenModalPopup('" + fullLink + "', '" + WidthPopup.ToString() + "', '" + HeightPopup.ToString() + "'); return false;");
                }
                else
                {
                    LnkButton.Attributes.Remove("onclick");
                    LnkButton.PostBackUrl = Link.ToString();
                }
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用OnPreRender方法。它将读取您在webpart编辑器部分中设置的属性。