Introduction
Here i created custom web part for google search in sharepoint C# 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.Drawing;
namespace googleSearch_webpart
{
public class google : WebPart //Inheritance with Webpart class
{
public Label label = new Label();
public TextBox textBox = new TextBox();
public Button button = new Button();
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.Text = "Google it";
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 CreateChildControls()
{
base.CreateChildControls();
}
void button_Click(object sender, EventArgs e)
{
string url = "https://www.google.co.in/?gws_rd=cr&ei=yAO4UuvhCMPorAf0jICoDg#q=" + textBox.Text;
Context.Response.Redirect(url);
}
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);
button.RenderControl(writer);
writer.Write(ospart4);
}
}
}
|