Question

I'm having trouble creating visual web part properties in visual studio 2012.. I was refered to http://msdn.microsoft.com/en-us/library/ee231551.aspx, but then was unsuccessful..

looked in the Web part and found the .ascx, ascx.cs and the ascx.g.cs

I've been trying to find the webpart.cs file but then I cant. Is there a way I can create it or maybe I'm looking in the wrong place..

Was it helpful?

Solution

There is no webpart .cs files in Visual Web Part created in VS 2012. You can add Web Part properties to ascx.cs files e.g.

     [WebBrowsable(true),
     WebDisplayName("Page Title"),
     WebDescription("Title displayed on the page"),
     Category("Test Properties"),
     Personalizable(PersonalizationScope.Shared)]
    public string PageTitle
    {
        get
        {
            return _pageTitle;
        }
        set
        {
            _pageTitle = value;
        }
    }

OTHER TIPS

The visual web part differs from asp.net web part where the code is located in ascx.cs file, and to add a property you should add the following code below class definition in your web part

[WebBrowsable(true),
WebDisplayName("Project ID"),
WebDescription("Enter Project Number"),
Personalizable(PersonalizationScope.Shared),
Category("Project Settings")]
public string ProjectID { get; set; }

For more details, please check the detail steps at Create a Custom Visual WebPart Property With Default Value in SharePoint

the solution to Content Query Web Part (CQWP) : toolpart. toolpart.cs public class ContentQueryToolPart : ToolPart { private DropDownList ddlMainXslTemplate; private ContentQuery webPart;

    public ContentQueryToolPart()
    {
        this.Title = "more props";
    }

    public override void ApplyChanges()
    {
        webPart.MainXslTemplate = (MainXslTemplateEnum)Enum.Parse(typeof(MainXslTemplateEnum), ddlMainXslTemplate.SelectedItem.Text);
    }

    protected override void OnInit(EventArgs e)
    {
        webPart = (ContentQuery)this.ParentToolPane.SelectedWebPart;
        base.OnInit(e);
    }

    protected override void CreateChildControls()
    {
        Label label = new Label();
        label.Text = "main xsl";
        this.Controls.Add(label);

        ddlMainXslTemplate = new DropDownList();
        ddlMainXslTemplate.DataSource = Enum.GetNames(typeof(MainXslTemplateEnum));
        ddlMainXslTemplate.DataBind();
        if (string.IsNullOrEmpty(webPart.MainXslTemplate.ToString()) == false)
        {
            ddlMainXslTemplate.SelectedValue = webPart.MainXslTemplate.ToString();
        }
        this.Controls.Add(ddlMainXslTemplate);
        //btw a custom UC can go here
    }

    protected override void RenderToolPart(System.Web.UI.HtmlTextWriter output)
    {
        output.AddAttribute(HtmlTextWriterAttribute.Id, "ContentQueryToolPart");
        output.RenderBeginTag(HtmlTextWriterTag.Div);
        base.RenderToolPart(output);
        output.RenderEndTag();
        //this is mainly for css 
    }
}

and in your webpart.cs

[ToolboxItemAttribute(false)]
public class ContentQuery : ContentByQueryWebPart
{
    public MainXslTemplateEnum MainXslTemplate { get; set; }

    public override ToolPart[] GetToolParts()
    {
        List<ToolPart> toolParts = new List<ToolPart>();
        toolParts.Add(new ContentQueryToolPart());
        toolParts.AddRange(base.GetToolParts());
        return toolParts.ToArray();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top