https://msdn.microsoft.com/es-es/library/office/hh857549(v=office.14).aspx
Code paste on Web Part in SharePoint 2007/2010
//varible declaration
// private string strResult;
protected TextBox txtSearch;
protected Button btnSearch;
protected Label lblErrMsg;
protected GridView GridSearch;
string CurrentURL = SPContext.Current.Web.Url;
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
// strResult += "CreateChildControls<br>"; //creating error label controls
this.lblErrMsg = new Label();
this.lblErrMsg.ID = "lblErrMsg";
this.lblErrMsg.Text = string.Empty;
this.Controls.Add(lblErrMsg); //creating text controls
this.txtSearch = new TextBox();
this.txtSearch.ID = "txtSearch";
this.txtSearch.Text = string.Empty;
this.Controls.Add(txtSearch); //creating button controls
this.btnSearch = new Button();
this.btnSearch.ID = "btnSearch";
this.btnSearch.Text = "Search";
this.btnSearch.Click += new EventHandler(btnSearch_Click);
this.Controls.Add(btnSearch);
//gridview Initialization
this.GridSearch = new GridView();
this.GridSearch.Width = Unit.Percentage(100);
this.GridSearch.AlternatingRowStyle.BackColor = System.Drawing.Color.LightGray;
this.GridSearch.EmptyDataText = "No records found.";
this.GridSearch.ID = "GridSearch";
this.GridSearch.Width = Unit.Percentage(100);
this.GridSearch.CellPadding = 0;
this.GridSearch.CellSpacing = 0;
//this.GridSearch.BorderWidth = 1;
this.GridSearch.HeaderStyle.Font.Bold = true;
this.GridSearch.BorderWidth = Unit.Pixel(1);
this.GridSearch.AllowPaging = true;
this.GridSearch.PageSize = 2;
this.GridSearch.AutoGenerateColumns = false;
BoundField id = new BoundField();
id.DataField = "ID";
id.HeaderText = "ID";
id.Visible = false;
this.GridSearch.Columns.Add(id);
//HyperLinkField title1 = new HyperLinkField();
//title1.DataTextField = "Title";
//title1.HeaderText = "Title";
//title1.NavigateUrl = "/Lists/PU/DispForm.aspx?ID=" + ID;
//title1.Target = "_blank";
//this.GridSearch.Columns.Add(title1);
BoundField title = new BoundField();
title.DataField = "Title";
title.HeaderText = "Title";
this.GridSearch.Columns.Add(title);
//TemplateField tfield = new TemplateField();
//tfield.HeaderText = "View";
//this.GridSearch.Columns.Add(tfield);
BoundField BU = new BoundField();
BU.DataField = "BU";
BU.HeaderText = "BU";
this.GridSearch.Columns.Add(BU);
BoundField technology = new BoundField();
technology.DataField = "Technology";
technology.HeaderText = "Technology";
this.GridSearch.Columns.Add(technology);
//BoundField startDate = new BoundField();
//startDate.DataField = "StartDate";
//startDate.HeaderText = "Start Date";
//startDate.DataFormatString = "{0:MMMM d, yyyy}";
//this.GridSearch.Columns.Add(startDate);
this.GridSearch.PageIndexChanging += new GridViewPageEventHandler(GridSearch_PageIndexChanging);
this.GridSearch.RowDataBound += new GridViewRowEventHandler(GridSearch_RowDataBound);
// this.GridSearch.RowDataBound += new GridViewPageEventHandler(GridSearch_RowDataBound);
this.Controls.Add(this.GridSearch);
}
catch (Exception ex)
{
this.lblErrMsg.Text = ex.Message.ToString();
}
}
void GridSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblID = new Label();
lblID.ID = "lblID";
lblID.Text = (e.Row.DataItem as DataRowView).Row["ID"].ToString();
e.Row.Cells[0].Controls.Add(lblID);
HyperLink hplTitle = new HyperLink();
hplTitle.ID = "hplTitle";
hplTitle.Text = (e.Row.DataItem as DataRowView).Row["Title"].ToString();
hplTitle.NavigateUrl = "/Lists/PU/DispForm.aspx?ID=" + lblID.Text.Trim();
hplTitle.Target = "_blank";
e.Row.Cells[1].Controls.Add(hplTitle);
Label lblBU = new Label();
lblBU.ID = "lblBU";
lblBU.Text = (e.Row.DataItem as DataRowView).Row["BU"].ToString();
e.Row.Cells[2].Controls.Add(lblBU);
Label lblTechnology = new Label();
lblTechnology.ID = "lblTechnology";
lblTechnology.Text = (e.Row.DataItem as DataRowView).Row["Technology"].ToString();
e.Row.Cells[3].Controls.Add(lblTechnology);
}
}
catch (Exception ex)
{
this.lblErrMsg.Text = ex.Message.ToString();
}
}
//protected void GridSearch_RowDataBound(object sender, GridViewRowEventArgs e)
//{
// //Label lblID;
// //HyperLink hplTitle;
// //Label lblBU;
// //Label lblTechnology;
// //HyperLinkField hplTitle;
// try
// {
// //if (e.Row.RowType == DataControlRowType.DataRow)
// //{
// // TextBox txtCountry = new TextBox();
// // txtCountry.ID = "txtCountry";
// // txtCountry.Text = (e.Row.DataItem as DataRowView).Row["Country"].ToString();
// // e.Row.Cells[1].Controls.Add(txtCountry);
// // LinkButton lnkView = new LinkButton();
// // lnkView.ID = "lnkView";
// // lnkView.Text = "View";
// // lnkView.Click += ViewDetails;
// // lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
// // e.Row.Cells[2].Controls.Add(lnkView);
// //}
// //if (e.Row.RowType == DataControlRowType.DataRow)
// //{
// // Label lblID = (Label)e.Row.FindControl("lblID");
// // HyperLink hplTitle = (HyperLink)e.Row.FindControl("hplTitle");
// // hplTitle.NavigateUrl = "/Lists/PU/DispForm.aspx?ID=" + lblID.Text.Trim();
// // Label lblBU = (Label)e.Row.FindControl("lblBU");
// // Label lblTechnology = (Label)e.Row.FindControl("lblTechnology");
// //}
// }
// catch (Exception ex)
// {
// this.lblErrMsg.Text = ex.Message.ToString();
// }
//}
protected void GridSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
if (ViewState["SearchPageIndexChanging"] != null)
{
GridSearch.PageIndex = e.NewPageIndex;
GridSearch.DataSource = ViewState["SearchPageIndexChanging"];
GridSearch.DataBind();
GridSearch.Visible = true;
}
}
catch (Exception ex)
{
this.lblErrMsg.Text = ex.Message.ToString();
}
}
public override void RenderControl(HtmlTextWriter writer)
{
try
{
//this method ensure all created child controls
base.CreateChildControls();
//this.strResult += "RenderControl<br>";
//table start
writer.Write("<table id='tblTest' width='770px' border='0' cellpadding='0' cellspacing='0'>");
writer.Write("<tr>");
writer.Write("<td align='left' style='padding-left:10px;'>");
this.txtSearch.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td>");
this.btnSearch.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td colspan='2' align='left' >");
this.lblErrMsg.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td colspan='2' align='left' style='padding-left:10px;' >");
this.GridSearch.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
//table end
}
catch (Exception ex)
{
this.lblErrMsg.Text = ex.Message.ToString();
}
}
void btnSearch_Click(object sender, EventArgs e)
{
try
{
getDatabind(txtSearch.Text.Trim());
}
catch (Exception ex)
{
lblErrMsg.Text = ex.Message;
}
}
public void getDatabind(string search)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(CurrentURL))
{
using (SPWeb web = site.OpenWeb())
{
//DataTable dt = new DataTable();
//dt.Clear();
//dt.Columns.Add("ID");
//dt.Columns.Add("Title");
//dt.Columns.Add("BU");
//dt.Columns.Add("Technology");
SPList list = web.Lists["PU"];
SPQuery query = new SPQuery();
ViewState["SearchPageIndexChanging"] = null;
query.Query = "<Where><Or><Contains><FieldRef Name='Title' /><Value Type='Text'>" + search + "</Value></Contains><Or><Contains><FieldRef Name='BU' /><Value Type='Text'>" + search + "</Value></Contains><Contains><FieldRef Name='Technology' /><Value Type='Text'>" + search + "</Value></Contains></Or></Or></Where>";
query.ViewFields = @"<FieldRef Name='ID' /><FieldRef Name='Title' /><FieldRef Name='BU' /><FieldRef Name='Technology' />";
query.ViewFieldsOnly = true;
//SPListItemCollection items = list.GetItems(query);
//if (list != null && list.Items.Count > 0)
//{
// foreach (SPListItem item in items)
// {
// DataRow dr = dt.NewRow();
// dr["ID"] = this.GetFieldValue(item, "ID");
// dr["Title"] = this.GetFieldValue(item, "Title"); //string.Concat(list.DefaultDisplayFormUrl, string.Format("?ID={0}", item.ID));
// dr["BU"] = this.GetFieldValue(item, "BU");
// dr["Technology"] = this.GetFieldValue(item, "Technology");
// dt.Rows.Add(dr);
// }
// if (dt != null && dt.Rows.Count > 0)
// {
// ViewState["SearchPageIndexChanging"] = dt;
// GridSearch.DataSource = dt;
// GridSearch.DataBind();
// GridSearch.Visible = true;
// }
// else
// {
// GridSearch.Visible = false;
// }
//}
SPListItemCollection item = list.GetItems(query);
DataTable dtd = item.GetDataTable();
if (dtd != null && dtd.Rows.Count > 0)
{
ViewState["SearchPageIndexChanging"] = dtd;
GridSearch.DataSource = dtd;
GridSearch.DataBind();
GridSearch.Visible = true;
}
else
{
GridSearch.Visible = false;
}
}
}
});
}
catch (Exception ex)
{
GridSearch.DataSource = null;
GridSearch.DataBind();
lblErrMsg.Text = ex.Message;
}
}
private string GetFieldValue(SPListItem item, string fieldName)
{
string strFieldValue = string.Empty;
if (item != null && item.Fields.ContainsField(fieldName) && item[fieldName] != null)
{
strFieldValue = item[fieldName].ToString();
}
return strFieldValue;
}
Code paste on Web Part in SharePoint 2007/2010
//varible declaration
// private string strResult;
protected TextBox txtSearch;
protected Button btnSearch;
protected Label lblErrMsg;
protected GridView GridSearch;
string CurrentURL = SPContext.Current.Web.Url;
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
// strResult += "CreateChildControls<br>"; //creating error label controls
this.lblErrMsg = new Label();
this.lblErrMsg.ID = "lblErrMsg";
this.lblErrMsg.Text = string.Empty;
this.Controls.Add(lblErrMsg); //creating text controls
this.txtSearch = new TextBox();
this.txtSearch.ID = "txtSearch";
this.txtSearch.Text = string.Empty;
this.Controls.Add(txtSearch); //creating button controls
this.btnSearch = new Button();
this.btnSearch.ID = "btnSearch";
this.btnSearch.Text = "Search";
this.btnSearch.Click += new EventHandler(btnSearch_Click);
this.Controls.Add(btnSearch);
//gridview Initialization
this.GridSearch = new GridView();
this.GridSearch.Width = Unit.Percentage(100);
this.GridSearch.AlternatingRowStyle.BackColor = System.Drawing.Color.LightGray;
this.GridSearch.EmptyDataText = "No records found.";
this.GridSearch.ID = "GridSearch";
this.GridSearch.Width = Unit.Percentage(100);
this.GridSearch.CellPadding = 0;
this.GridSearch.CellSpacing = 0;
//this.GridSearch.BorderWidth = 1;
this.GridSearch.HeaderStyle.Font.Bold = true;
this.GridSearch.BorderWidth = Unit.Pixel(1);
this.GridSearch.AllowPaging = true;
this.GridSearch.PageSize = 2;
this.GridSearch.AutoGenerateColumns = false;
BoundField id = new BoundField();
id.DataField = "ID";
id.HeaderText = "ID";
id.Visible = false;
this.GridSearch.Columns.Add(id);
//HyperLinkField title1 = new HyperLinkField();
//title1.DataTextField = "Title";
//title1.HeaderText = "Title";
//title1.NavigateUrl = "/Lists/PU/DispForm.aspx?ID=" + ID;
//title1.Target = "_blank";
//this.GridSearch.Columns.Add(title1);
BoundField title = new BoundField();
title.DataField = "Title";
title.HeaderText = "Title";
this.GridSearch.Columns.Add(title);
//TemplateField tfield = new TemplateField();
//tfield.HeaderText = "View";
//this.GridSearch.Columns.Add(tfield);
BoundField BU = new BoundField();
BU.DataField = "BU";
BU.HeaderText = "BU";
this.GridSearch.Columns.Add(BU);
BoundField technology = new BoundField();
technology.DataField = "Technology";
technology.HeaderText = "Technology";
this.GridSearch.Columns.Add(technology);
//BoundField startDate = new BoundField();
//startDate.DataField = "StartDate";
//startDate.HeaderText = "Start Date";
//startDate.DataFormatString = "{0:MMMM d, yyyy}";
//this.GridSearch.Columns.Add(startDate);
this.GridSearch.PageIndexChanging += new GridViewPageEventHandler(GridSearch_PageIndexChanging);
this.GridSearch.RowDataBound += new GridViewRowEventHandler(GridSearch_RowDataBound);
// this.GridSearch.RowDataBound += new GridViewPageEventHandler(GridSearch_RowDataBound);
this.Controls.Add(this.GridSearch);
}
catch (Exception ex)
{
this.lblErrMsg.Text = ex.Message.ToString();
}
}
void GridSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblID = new Label();
lblID.ID = "lblID";
lblID.Text = (e.Row.DataItem as DataRowView).Row["ID"].ToString();
e.Row.Cells[0].Controls.Add(lblID);
HyperLink hplTitle = new HyperLink();
hplTitle.ID = "hplTitle";
hplTitle.Text = (e.Row.DataItem as DataRowView).Row["Title"].ToString();
hplTitle.NavigateUrl = "/Lists/PU/DispForm.aspx?ID=" + lblID.Text.Trim();
hplTitle.Target = "_blank";
e.Row.Cells[1].Controls.Add(hplTitle);
Label lblBU = new Label();
lblBU.ID = "lblBU";
lblBU.Text = (e.Row.DataItem as DataRowView).Row["BU"].ToString();
e.Row.Cells[2].Controls.Add(lblBU);
Label lblTechnology = new Label();
lblTechnology.ID = "lblTechnology";
lblTechnology.Text = (e.Row.DataItem as DataRowView).Row["Technology"].ToString();
e.Row.Cells[3].Controls.Add(lblTechnology);
}
}
catch (Exception ex)
{
this.lblErrMsg.Text = ex.Message.ToString();
}
}
//protected void GridSearch_RowDataBound(object sender, GridViewRowEventArgs e)
//{
// //Label lblID;
// //HyperLink hplTitle;
// //Label lblBU;
// //Label lblTechnology;
// //HyperLinkField hplTitle;
// try
// {
// //if (e.Row.RowType == DataControlRowType.DataRow)
// //{
// // TextBox txtCountry = new TextBox();
// // txtCountry.ID = "txtCountry";
// // txtCountry.Text = (e.Row.DataItem as DataRowView).Row["Country"].ToString();
// // e.Row.Cells[1].Controls.Add(txtCountry);
// // LinkButton lnkView = new LinkButton();
// // lnkView.ID = "lnkView";
// // lnkView.Text = "View";
// // lnkView.Click += ViewDetails;
// // lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
// // e.Row.Cells[2].Controls.Add(lnkView);
// //}
// //if (e.Row.RowType == DataControlRowType.DataRow)
// //{
// // Label lblID = (Label)e.Row.FindControl("lblID");
// // HyperLink hplTitle = (HyperLink)e.Row.FindControl("hplTitle");
// // hplTitle.NavigateUrl = "/Lists/PU/DispForm.aspx?ID=" + lblID.Text.Trim();
// // Label lblBU = (Label)e.Row.FindControl("lblBU");
// // Label lblTechnology = (Label)e.Row.FindControl("lblTechnology");
// //}
// }
// catch (Exception ex)
// {
// this.lblErrMsg.Text = ex.Message.ToString();
// }
//}
protected void GridSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
if (ViewState["SearchPageIndexChanging"] != null)
{
GridSearch.PageIndex = e.NewPageIndex;
GridSearch.DataSource = ViewState["SearchPageIndexChanging"];
GridSearch.DataBind();
GridSearch.Visible = true;
}
}
catch (Exception ex)
{
this.lblErrMsg.Text = ex.Message.ToString();
}
}
public override void RenderControl(HtmlTextWriter writer)
{
try
{
//this method ensure all created child controls
base.CreateChildControls();
//this.strResult += "RenderControl<br>";
//table start
writer.Write("<table id='tblTest' width='770px' border='0' cellpadding='0' cellspacing='0'>");
writer.Write("<tr>");
writer.Write("<td align='left' style='padding-left:10px;'>");
this.txtSearch.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td>");
this.btnSearch.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td colspan='2' align='left' >");
this.lblErrMsg.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td colspan='2' align='left' style='padding-left:10px;' >");
this.GridSearch.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
//table end
}
catch (Exception ex)
{
this.lblErrMsg.Text = ex.Message.ToString();
}
}
void btnSearch_Click(object sender, EventArgs e)
{
try
{
getDatabind(txtSearch.Text.Trim());
}
catch (Exception ex)
{
lblErrMsg.Text = ex.Message;
}
}
public void getDatabind(string search)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(CurrentURL))
{
using (SPWeb web = site.OpenWeb())
{
//DataTable dt = new DataTable();
//dt.Clear();
//dt.Columns.Add("ID");
//dt.Columns.Add("Title");
//dt.Columns.Add("BU");
//dt.Columns.Add("Technology");
SPList list = web.Lists["PU"];
SPQuery query = new SPQuery();
ViewState["SearchPageIndexChanging"] = null;
query.Query = "<Where><Or><Contains><FieldRef Name='Title' /><Value Type='Text'>" + search + "</Value></Contains><Or><Contains><FieldRef Name='BU' /><Value Type='Text'>" + search + "</Value></Contains><Contains><FieldRef Name='Technology' /><Value Type='Text'>" + search + "</Value></Contains></Or></Or></Where>";
query.ViewFields = @"<FieldRef Name='ID' /><FieldRef Name='Title' /><FieldRef Name='BU' /><FieldRef Name='Technology' />";
query.ViewFieldsOnly = true;
//SPListItemCollection items = list.GetItems(query);
//if (list != null && list.Items.Count > 0)
//{
// foreach (SPListItem item in items)
// {
// DataRow dr = dt.NewRow();
// dr["ID"] = this.GetFieldValue(item, "ID");
// dr["Title"] = this.GetFieldValue(item, "Title"); //string.Concat(list.DefaultDisplayFormUrl, string.Format("?ID={0}", item.ID));
// dr["BU"] = this.GetFieldValue(item, "BU");
// dr["Technology"] = this.GetFieldValue(item, "Technology");
// dt.Rows.Add(dr);
// }
// if (dt != null && dt.Rows.Count > 0)
// {
// ViewState["SearchPageIndexChanging"] = dt;
// GridSearch.DataSource = dt;
// GridSearch.DataBind();
// GridSearch.Visible = true;
// }
// else
// {
// GridSearch.Visible = false;
// }
//}
SPListItemCollection item = list.GetItems(query);
DataTable dtd = item.GetDataTable();
if (dtd != null && dtd.Rows.Count > 0)
{
ViewState["SearchPageIndexChanging"] = dtd;
GridSearch.DataSource = dtd;
GridSearch.DataBind();
GridSearch.Visible = true;
}
else
{
GridSearch.Visible = false;
}
}
}
});
}
catch (Exception ex)
{
GridSearch.DataSource = null;
GridSearch.DataBind();
lblErrMsg.Text = ex.Message;
}
}
private string GetFieldValue(SPListItem item, string fieldName)
{
string strFieldValue = string.Empty;
if (item != null && item.Fields.ContainsField(fieldName) && item[fieldName] != null)
{
strFieldValue = item[fieldName].ToString();
}
return strFieldValue;
}
No comments:
Post a Comment