Tuesday, 4 April 2017

Search and HeilightSearch Result Bound In ListView Control In Asp.Net Using C#.Net

4/04/2017 - By Pranav Singh 2

This article will show you how you can highlight the listview bound control row after search in asp.net using c#.net on row data bound.

So for this article first we will  create an asp.net application and add the below control on page.

<form id="form1" runat="server">
        <div>
            <table widtrh="100%">
                <tr>
                    <td>
                        <asp:TextBox ID="txtSearchContent" runat="server"></asp:TextBox>
                        <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound">
                            <LayoutTemplate>
                                <table width="100%">
                                    <tr style="background-color: gray;">
                                        <th>Name</th>
                                        <th>Class</th>
                                        <th>Section</th>
                                        <th>Marks</th>
                                    </tr>
                                    <tr id="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <tr runat="server" id="listViewControlData">
                                    <td><%# Eval("Name") %></td>
                                    <td><%# Eval("Class") %></td>
                                    <td><%# Eval("Section") %></td>
                                    <td><%# Eval("Marks") %></td>
                                </tr>
                            </ItemTemplate>
                        </asp:ListView>
                    </td>
                </tr>
            </table>

        </div>
    </form>
In above code just check the highlight part of the code this is one of the important part of the code., if we not do this on that case we will not be able to bind the listview control and will get the below mention error.



 Now open the .cs page and add the below class.

public class StudentData
    {
        public string Name { get; set; }
        public int Class { get; set; }
        public string Section { get; set; }
        public int Marks { get; set; }
    }

This class file we will use for making the data collection. Now lets bind the listview control. Please check the complete code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class LiteralHeilight : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindListControl();
            }
        }

        ///


        /// Code to bind data to list view control
          public void BindListControl()
        {
            List<StudentData> _studentData = new List<StudentData>();
            _studentData = GetStudentData();
            ListView1.DataSource = _studentData;
            ListView1.DataBind();
        }

        ///
        /// This function is used for getting the student data to bind
        ///

        ///
        public List<StudentData> GetStudentData()
        {
            List<StudentData> _studentData = new List<StudentData>();
            //Add some record to literal. You can access data from DB and store into the List
            _studentData.Add(new StudentData { Name = "Rajesh", Class = 10, Marks = 70, Section = "A" });
            _studentData.Add(new StudentData { Name = "Umesh", Class = 11, Marks = 80, Section = "B" });
            _studentData.Add(new StudentData { Name = "Mark", Class = 5, Marks = 90, Section = "C" });
            _studentData.Add(new StudentData { Name = "Max", Class = 12, Marks = 60, Section = "D" });
            _studentData.Add(new StudentData { Name = "Joyti", Class = 11, Marks = 87, Section = "A" });
            return _studentData;
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (txtSearchContent.Text != "")
            {
                string srch = txtSearchContent.Text.Trim().ToLower();
                var _data = (e.Item as ListViewDataItem).DataItem as StudentData;
                if ((!string.IsNullOrEmpty(_data.Name) && _data.Name.ToLower().Contains(srch)))
                {
                    HtmlTableRow _htmlTableRow = e.Item.FindControl("listViewControlData"as HtmlTableRow;
                    _htmlTableRow.BgColor = "Yellow";
                }
            }
        }

        ///
        /// Click event
        ///

        /// to search record and bind
        ///
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindListControl();
        }
    }
    public class StudentData
    {
        public string Name { getset; }
        public int Class { getset; }
        public string Section { getset; }
        public int Marks { getset; }
    }
}

      

In above code I have bind the listview control to the list data of student. Now let check the below mention code.

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (txtSearchContent.Text != "")
            {
                string srch = txtSearchContent.Text.Trim().ToLower();
                var _data = (e.Item as ListViewDataItem).DataItem as StudentData;
                if ((!string.IsNullOrEmpty(_data.Name) && _data.Name.ToLower().Contains(srch)))
                {
                    HtmlTableRow _htmlTableRow = e.Item.FindControl("listViewControlData") as HtmlTableRow;
                    _htmlTableRow.BgColor = "Yellow";
                }
            }
        }



This code is used for highlighting the search row.  Now we have done run the application to check the output.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. Thank u Somuch bro..
    Superb..
    Very Use full artical are Avaliable in your page

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top