简单的可视化webpart,不呈现控件

时间:2013-05-16 12:14:18

标签: c# sharepoint sharepoint-2010 sharepoint-2013

我用这个html创建了一个简单的可视化webpart

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LinkButton.ascx.cs" Inherits="xx.xxxxDMS.WebParts.VisualWebParts.LinkButton.LinkButton" %>
<script type="text/javascript">
    function OpenModalPopup(pageUrl) {
        var options = { url: pageUrl, width: 900, height: 300 };
        SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
    }
</script>
<asp:LinkButton ID="LnkButton" runat="server"></asp:LinkButton>

然后在后面的代码中,我添加了3个属性。在使用文本链接和URL编辑属性后,我没有看到LinkBut​​ton呈现。我错过了什么吗?请注意其SP 2013。

我打算尝试这个解决方案:http://www.tfsconsulting.com.au/visual-studio-2012-sharepoint-2013-visual-web-part-project-template-is-buggy/

但是我注意到我的customwebpart在控制模板文件夹中没有ASCX文件,所以我猜这在2013年有所改变?

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

namespace xx.SP.xx.WebParts.VisualWebParts.LinkButton
{
    [ToolboxItemAttribute(false)]
    public partial class LinkButton : WebPart
    {
        private string _LinkText;
        private Uri _Link;
        private Boolean _OpenModal;    

        [WebBrowsable(true), WebDisplayName("LinkText"), WebDescription("Text for the link"),
        Personalizable(PersonalizationScope.Shared), Category("xx - xx"),
        System.ComponentModel.DefaultValue("")]
        public string LinkText
        {
            get { return _LinkText; }
            set { _LinkText = value; }
        }

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

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

        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public LinkButton()
        {
        }

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

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void CreateChildControls()
        {
            if (LnkButton != null)
            {
                LnkButton.Text = LinkText;
                if (OpenModal)
                {
                    LnkButton.Attributes.Add("onclick", "OpenModalPopup('" + Link.ToString() + "');");
                }
                else
                {
                    LnkButton.PostBackUrl = Link.ToString();
                }
            }           
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Sharepoint 2013 webparts的工作方式不同。 不需要使用控件模板找到控件,如果将代码从CreateChildControls移动到页面加载,它将正常工作。