Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
Custom webpart using custom properties example- sharepoint
2953 Views
Introduction 
I have created search webpart in Custom coding here.In Tool pane you can change the property of the webpart whether google search or yahoo search or Bing search or you can click the checkbox to give your search url.I have done this by using custom properties and also i have used all the custom properties in this example like enum,string,bool.
Look of the tool pane with custom properties 
Code
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Web.UI;
	using System.Web.UI.WebControls.WebParts;
	using System.Web.UI.WebControls;
	using Microsoft.SharePoint;
	using System.Xml.Serialization;
	using System.Drawing;
	using System.ComponentModel;
	using Microsoft.SharePoint.WebPartPages;
	
	namespace googleSearch_webpart
	{
	   [DefaultProperty("Text"),
	       ToolboxData("<{0}:CustomPropertyWebPart runat=server></{0}:CustomPropertyWebPart>"),
	       XmlRoot(Namespace = "googleSearch_webpart")]
	   public class google : System.Web.UI.WebControls.WebParts.WebPart
	   {
	       public Label label = new Label();
	       public TextBox textBox = new TextBox();
	       public Button button = new Button();
	       string url = "";
	       public int i = 0;
	       const bool c_bool = false;
	       public enum searchform
	       {
	           google = 0,
	           yahoo,
	           bing
	       };
	       private bool _bool;
	       private string _address;
	       private string _adlabel;
	       private searchform _mySearch;
	       public google()
	       {
	           _bool = c_bool;
	       }
	       [SPWebCategoryName("Custom Properties")]
	       [DefaultValue(searchform.google)]
	       [WebPartStorage(Storage.Shared)]
	       [FriendlyName("Select Browsers")]
	       [WebDescription("Select a value from the dropdown list.")]
	       [WebBrowsable(true)]
	       [Personalizable(PersonalizationScope.Shared)]
	       [XmlElement(ElementName = "mySearch")]
	       public searchform mySearch
	       {
	           get
	           {
	               return _mySearch;
	           }
	           set
	           {
	               _mySearch = value;
	           }
	       }
	       [SPWebCategoryName("Custom Properties")]
	       [DefaultValue(c_bool)]
	       [WebPartStorage(Storage.Shared)]
	       [FriendlyName("Click this to give custom URL and Label")]
	       [WebDescription("Click this to give custom URL and Label")]
	       [WebBrowsable(true)]
	       [Personalizable(PersonalizationScope.Shared)]
	       [XmlElement(ElementName = "Bool")]
	       public bool Bool
	       {
	           get
	           {
	               return _bool;
	           }
	           set
	           {
	               _bool = value;
	           }
	       }
	       [SPWebCategoryName("Custom Properties")]
	       [WebPartStorage(Storage.Shared)]
	       [FriendlyName("Search URL")]
	       [WebDescription("Search URL")]
	       [WebBrowsable(true)]
	       [Personalizable(PersonalizationScope.Shared)]
	       [XmlElement(ElementName = "Address")]
	       public string Address
	       {
	           get
	           {
	               return _address;
	           }
	           set
	           {
	               _address = value;
	           }
	       }
	       [SPWebCategoryName("Custom Properties")]
	       [WebPartStorage(Storage.Shared)]
	       [FriendlyName("Search label")]
	       [WebDescription("Search label")]
	       [WebBrowsable(true)]
	       [Personalizable(PersonalizationScope.Shared)]
	       [XmlElement(ElementName = "Adlabel")]
	       public string Adlabel
	       {
	           get
	           {
	               return _adlabel;
	           }
	           set
	           {
	               _adlabel = value;
	           }
	       }
	       protected override void OnInit(EventArgs e)
	       {
	           base.OnInit(e);
	           label.ID = "search";
	           label.Text = "Search Term";
	           label.Font.Bold = true;
	           Controls.Add(label);
	           textBox.ID = "googleBox";
	           textBox.Text = "";
	           textBox.Width = Unit.Pixel(80);
	           textBox.BorderColor = Color.FromArgb(79, 129, 189);
	           textBox.BorderWidth = Unit.Pixel(2);
	           Controls.Add(textBox);
	           button.Click += new EventHandler(button_Click);
	           button.ID = "button1";
	           button.ForeColor = Color.White;
	           button.Font.Bold = true;
	           button.BackColor = Color.FromArgb(79, 129, 189);
	           button.BorderColor = Color.FromArgb(56, 93, 138);
	           button.CausesValidation = true;
	           Controls.Add(button);
	       }
	       protected override void OnLoad(EventArgs e)
	       {
	           base.OnLoad(e);
	       }
	       protected override void CreateChildControls()
	       {
	           base.CreateChildControls();
	       }
	       void button_Click(object sender, EventArgs e)
	       {
	           i = 1;
	           if (_bool == false)
	           {
	               if (_mySearch.ToString() == "google")
	                   url = "https://www.google.co.in/search?q=" + textBox.Text;
	               else if (_mySearch.ToString() == "yahoo")
	                   url = "http://in.search.yahoo.com/search?p=" + textBox.Text;
	               else
	                   url = "http://www.bing.com/search?q=" + textBox.Text;
	           }
	           else
	           {
	               url = _address + textBox.Text;
	           }
	       }
	       protected override void Render(HtmlTextWriter writer)
	       {
	           EnsureChildControls();
	           StringBuilder ospart1 = new StringBuilder();
	           StringBuilder ospart2 = new StringBuilder();
	           StringBuilder ospart3 = new StringBuilder();
	           StringBuilder ospart4 = new StringBuilder();
	           ospart1.Append("<div id=div1>");
	           ospart1.Append("<table width=200 cellspacing=5 cellpadding=3>");
	           ospart1.Append("<tr width=100%>");
	           ospart1.Append("<td width=50% align=center valign=center>");
	           ospart2.Append("</td>");
	           ospart2.Append("<td width=50% align=center valign=center>");  
	           ospart3.Append("</td></tr>");
	           ospart3.Append("<tr width=100%>");
	           ospart3.Append("<td width=100% height=50 align=center valign=center colspan=2>");
	           ospart4.Append("</td></tr></table></div>");
	           writer.Write(ospart1);
	           label.RenderControl(writer);
	           writer.Write(ospart2);
	           textBox.RenderControl(writer);
	           writer.Write(ospart3);
	           if (_bool == false)
	           {
	               if (_mySearch.ToString() == "google")
	               {
	                   button.Text = "Google it";
	               }
	               else if (_mySearch.ToString() == "yahoo")
	                   button.Text = "Yahoo it";
	               else
	                   button.Text = "Bing it";
	           }
	           else
	               button.Text = _adlabel;
	           button.RenderControl(writer);
	           writer.Write(ospart4);
	           if(i==1)
	           writer.Write("<iframe id=Frame src="+url+" width=100% height=400></iframe>");
	       }
	   }
	}
Previous Topics
Previous lessons of current book.
Best Lessons of "Microsoft sharepoint"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details