1

I am making a visual webpart with custom properties. Basically its a link button which Text and Url properties can be set by the user via web part properties.

All samples I have seen say that in vs 2010(SP 2010), visual studio adds automatically and ascx path variable, but its not the case in 2013. I wonder if I have to do it the same way in 2013 or if it changed?

http://onlinecoder.blogspot.be/2011/04/custom-properties-in-sharepoint-2010_7158.html

My code:

 public partial class LinkButton : WebPart
    {

        private string _LinkText;
        private Uri _Link;


        [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; }
        }

        // 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()
        {
            LinkButton linkButton = Page.LoadControl(_ascxPath) as LinkButton;
            if (linkButton != null)
            {
                linkButton.Title = LinkText;
                linkButton.TitleUrl = Link.ToString();
                Controls.Add(linkButton);
            }           
        }
    }
1

You can add it manually

 private const string _ascxPath = "~/_CONTROLTEMPLATES/yourproject/yourcontrol.ascx";

If you cannot find "_CONTROLTEMPLATES" folder in your project,You should add mapped folders to your project

|improve this answer|||||
  • 1
    For 2013 don't forget the /15 after _CONTROLTEMPLATES – Robert Lindgren May 16 '13 at 9:49
  • but, the questions is, do I really need to? It looks vs 2012 changed the way to do visual web parts for sp 2013. If I add for example a control to the ascx file, called linkbutton, in the CreateChildControls Method I can do just like if (LnkButton != null) { LnkButton.Text = LinkText; LnkButton.Attributes.Add("onclick", "OpenModalPopup('" + Link.ToString() + "');"); } – Luis Valencia May 16 '13 at 9:54
1

I moved the code from the CreateChildrenCOntrols to the page load and then it works without any problem, visual webparts apparently work a little bit different in sp2013 but there is no documentation around.

THere is no need to find the controls using the controltemplates folder, the webparts are not even deployed there

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.