Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
Creating a Search Web Part with Custom Properties
2670 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.I have done this by using custom properties.
Look of the tool pane with custom properties 


Black Box covered portion created by the following code. 
Refer this Url to get complete idea about Custom Properties 
http://msdn.microsoft.com/en-us/library/dd584174(v=office.11).aspx
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;
	       public enum searchform
	       {
	           google = 0,
	           yahoo,
	           bing
	       };
	       private searchform _mySearch;
	       [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)]
	       public searchform mySearch
	       {
	           get
	           {
	               return _mySearch;
	           }
	           set
	           {
	               _mySearch = 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(_mySearch.ToString()=="google")
	           url = "https://www.google.co.in/?gws_rd=cr&ei=yAO4UuvhCMPorAf0jICoDg#q=" + textBox.Text;
	           else if(_mySearch.ToString()=="yahoo")
	               url = "http://in.search.yahoo.com/search;_ylt=A86.J_tLG7xSk_wAzYCuitIF?p=" + textBox.Text;
	           else
	               url = "http://www.bing.com/search?q=" + 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 (_mySearch.ToString() == "google")
	           {
	               button.Text = "Google it";
	           }
	           else if (_mySearch.ToString() == "yahoo")
	               button.Text = "Yahoo it";
	           else
	               button.Text = "Bing it";
	           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