【问题标题】:Correct order of methods in a webpart?Webpart 中方法的正确顺序?
【发布时间】:2013-05-31 20:04:49
【问题描述】:

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

我有一个 web 部件,它应该添加一个带有一些属性、文本和 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();
                }
            }
        }

    }
}

【问题讨论】:

    标签: c# sharepoint web-parts


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 2021-12-06
      相关资源
      最近更新 更多