Monday, October 14, 2013

Custom properties for visual webpart 2010

Custom properties for visual webpart
You should read 3 article first
http://mstechsharing.blogspot.com/2012/03/lesson-04-webpart-property-and-default.html
http://mstechsharing.blogspot.com/2012/03/lesson-05-more-attribute-and-custom.html
http://mstechsharing.blogspot.com/2012/03/lesson-06-validation-in-webpart-and.html

Create new Visual webpart project


Add new class for toolpart class

Open CustomPropertiesForVisualWebpart.cs and Replace this code
using System.ComponentModel;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;

namespace CustomPropertiesForVisualWebpart.CustomPropertiesForVisualWebpart
{
    [ToolboxItemAttribute(false)]
    public class CustomPropertiesForVisualWebpart : WebPart
    {
     
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/CustomPropertiesForVisualWebpart/CustomPropertiesForVisualWebpart/CustomPropertiesForVisualWebpartUserControl.ascx";

        protected override void CreateChildControls()
        {
            CustomPropertiesForVisualWebpartUserControl control = (CustomPropertiesForVisualWebpartUserControl)Page.LoadControl(_ascxPath);
            //Assign value to Attribute of webpart user control
            control.Attributes.Add("DisplayCar", DisplayCar);
            Controls.Add(control);
        }

        #region properties
        public string _value;// public static string _value; (Note: remove static)
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        string displayCar = "1";
        public string DisplayCar
        {
            get { return displayCar; }
            set { displayCar = value; }
        }
        #endregion
        /// <summary>
        /// return all toolpat
        /// </summary>
        /// <returns>return toolpart</returns>
        public override ToolPart[] GetToolParts()
        {
            ToolPart[] allToolParts = new ToolPart[3];
            WebPartToolPart standardToolParts = new WebPartToolPart();
            CustomPropertyToolPart customToolParts = new CustomPropertyToolPart();
            allToolParts[0] = standardToolParts;
            allToolParts[1] = customToolParts;
            allToolParts[2] = new CustomToolPart();
            return allToolParts;
        }
    }
}
Open CustomToolPart and replcae this code
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Data;


namespace CustomPropertiesForVisualWebpart.CustomPropertiesForVisualWebpart
{
    public class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    {
        #region variable
        /// <summary>
        /// declare variable
        /// </summary>
        Label lblError;
        DropDownList ddlDisplayCar;
        TextBox txtValue;
        #endregion
        /// <summary>
        /// Create Control
        /// </summary>
        protected override void CreateChildControls()
        {
            try
            {
                lblError = new Label();
                Label lblDisplayCar = new Label();
                lblDisplayCar.Text = "Choose display car: <br />";
                ddlDisplayCar = new DropDownList();
                ddlDisplayCar.ID = "ddlDisplayCar";
                ddlDisplayCar.Items.Add(new ListItem("All Cars", "1"));
                ddlDisplayCar.Items.Add(new ListItem("All Cars of Current User", "2"));
                txtValue = new TextBox();

                this.Controls.Add(lblError);
                this.Controls.Add(lblDisplayCar);
                this.Controls.Add(ddlDisplayCar);
                this.Controls.Add(txtValue);
                DisplayDataFromWebpartToToolPart();
            }
            catch (System.Exception ex)
            {
                lblError.Text = ex.Message;
                //You can write log here
            }
        }
        /// <summary>
        /// Display Data From Webpart To ToolPart
        /// </summary>
        private void DisplayDataFromWebpartToToolPart()
        {
            //Display data from webpart to toolpart
            CustomPropertiesForVisualWebpart customPropertiesVisualWP = (CustomPropertiesForVisualWebpart)this.ParentToolPane.SelectedWebPart;
            // DisplayCar
            ListItem currentDisplayCar = ddlDisplayCar.Items.FindByValue(customPropertiesVisualWP.DisplayCar);
            if (null != currentDisplayCar)
            {
                ddlDisplayCar.SelectedValue = currentDisplayCar.Value;
            }

            txtValue.Text = customPropertiesVisualWP.Value;
        }
        /// <summary>
        /// Apply changes when we click button Apply and Ok in toolpart panel
        /// </summary>
        public override void ApplyChanges()
        {
            CustomPropertiesForVisualWebpart customPropertiesVisualWP = (CustomPropertiesForVisualWebpart)this.ParentToolPane.SelectedWebPart;
            customPropertiesVisualWP.DisplayCar = ddlDisplayCar.SelectedValue;
            customPropertiesVisualWP.Value = txtValue.Text;
        }
    }
}
Open CustomPropertiesForVisualWebpartUserControl.ascx and Add 2 Label then open CustomPropertiesForVisualWebpartUserControl.ascx.cs and replcae this code
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace CustomPropertiesForVisualWebpart.CustomPropertiesForVisualWebpart
{
    public partial class CustomPropertiesForVisualWebpartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblCarWay1.Text = Convert.ToString(this.Attributes["DisplayCar"]);
            lblCarWay2.Text = CustomPropertiesForVisualWebpart._value;
        }
    }
}
Build and Deploy then add webpart => edit properties webpart


2 comments:


  1. Thank you for providing the information. I would like to see some more blogs on this topic..properties

    ReplyDelete
  2. Good c# class very nice post by admin.more about web development,web designing visit uk best webdesign company web design company uk

    ReplyDelete