Friday, November 20, 2009

webpart searching for List items using Textbox and Botton

<em><code>
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;


namespace samplewebpart
{
public class Serach : WebPart
{
TextBox txt1 = new TextBox();
protected override void CreateChildControls()
{

txt1.ID = "txt1";
this.Controls.Add(txt1);
Button btn1 = new Button();
btn1.ID = "btn1";
btn1.Click += new EventHandler(btn1_Click);
this.Controls.Add(btn1);





}

protected void btn1_Click(object sender, EventArgs e)
{
using (SPSite oSite = new SPSite("http://server1:1234/sites/sample/default.aspx"))
{
using (SPWeb oWeb = oSite.OpenWeb())
{

SPList oList = oWeb.Lists["TaskList"];
SPQuery query = new SPQuery();

query.Query = "<Where>"
+"<Contains>"
+ "<FieldRef Name='Developer' />"
+ "<Value Type='Text'>"
+ txt1.Text
+ "</Value>"
+ "</Contains>"
+ "</Where>";
SPListItemCollection items = oList.GetItems(query);
GridView GridView1 = new GridView();
GridView1.DataSource = items.GetDataTable();
GridView1.DataBind();
this.Controls.Add(GridView1);

}
}

}
}
}
</code>.</em>