0

在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();
                }
            }
        }

    }
}
4

1 回答 1

0

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

于 2013-06-05T08:41:10.860 回答