public partial class TestPage : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ListView1.ItemDataBound += new EventHandler<ListViewItemEventArgs>(ListView1_ItemDataBound);
if (!IsPostBack)
BindListView();
}
void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (!string.IsNullOrEmpty(txtSearch.Text) &&
txtSearch.Text.Trim().Length > 0)
{
string srch = txtSearch.Text.Trim().ToLower();
var _data = (e.Item as ListViewDataItem).DataItem as Sample;
if ((!string.IsNullOrEmpty(_data.Name) &&
_data.Name.ToLower().Contains(srch)) || (!string.IsNullOrEmpty(_data.Country) &&
_data.Country.ToLower().Contains(srch)))
{
HtmlTableRow abc = e.Item.FindControl("_itemrow") as HtmlTableRow;
abc.BgColor = "Green";
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
BindListView();
}
private void BindListView()
{
ListView1.DataSource = GetSampleList();
ListView1.DataBind();
}
private List<Sample> GetSampleList()
{
// build a sample collection to bind in list view
List<Sample> lst = new List<Sample>();
lst.Add(new Sample { Country = "India", Name = "Dotnetfunda" });
lst.Add(new Sample { Country = "Usa", Name = "abc def" });
lst.Add(new Sample { Country = "Singapore", Name = "hello z" });
lst.Add(new Sample { Country = "UK", Name = "salman khan" });
return lst;
}
}
public class Sample
{
public string Name { get;set; }
public string Country { get; set; }
}