Custom visual web part properties sharepoint

帅比萌擦擦* 提交于 2019-12-11 02:18:00

问题


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..


回答1:


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



回答2:


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

also, you can check the detail steps in this article

https://blog.devoworx.net/2016/03/23/create-custom-web-part-properties-for-visual-webpart-in-sharepoint-2010-2013/

Hope it helps you




回答3:


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


来源:https://stackoverflow.com/questions/19490046/custom-visual-web-part-properties-sharepoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!