正确的web部件中方法的顺序

本文关键字:方法 顺序 web | 更新日期: 2023-09-27 18:17:03

在web部件开发中,有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();
                }
            }
        }
    }
}

正确的web部件中方法的顺序

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