0

I have a gridview bound with data. I want to filter the records on button click and it will take some input from textbox and on button click it will filter the records and will display which records match with the input.

3
  • 1
    Please clear your question Commented Jul 6, 2015 at 13:35
  • Shortly I want a client side javascript code that will filter the records in a gridview which are similar to the input given in textbox.But I want this to happen on button click. Not on textbox 'onkeyup' event. Because I googled a lot and found the code for keyup event. I want the code for button click event. Commented Jul 6, 2015 at 13:39
  • the javascript function that you have found on 'onkeyup' event call this function on button 'onclick' attribute Commented Jul 6, 2015 at 13:46

1 Answer 1

1

I Think This Will Be Helpful For You...

ASPX Design :

<asp:Button ID="btnPopulate" runat="server" Text="Populate Grid" 
    onclick="btnPopulate_Click" /><br /><br />
<asp:GridView ID="gvDemo" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="First Name" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name" />
    </Columns>
</asp:GridView>
<br />
<asp:TextBox ID="txtKeyword" runat="server" Text=""></asp:TextBox>
<asp:Button ID="btnFilter" OnClientClick="Filter();return false;" runat="server" Text="Filter" />

JavaScript Code :

<script type="text/javascript">

    function Filter() {
        var txtKeyword = document.getElementById('<%= txtKeyword.ClientID %>');        
        var tblGrid = document.getElementById('<%= gvDemo.ClientID %>');        
        var rows = tblGrid.rows;
        var i = 0, row, cell ;
        for (i = 1; i < rows.length; i++) {
            row = rows[i];
            var found = false;
            for (var j = 0; j < row.cells.length; j++) {
                cell = row.cells[j];
                if (cell.innerHTML.toUpperCase().indexOf(txtKeyword.value.toUpperCase()) >= 0) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                row.style['display'] = 'none';

            }
            else {
                row.style.display = '';
            }
        }

        return false;
    }

</script>

C# Code :

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnPopulate_Click(object sender, EventArgs e)
{
    DataTable dtEmployee = PopulateDataTable();
    gvDemo.DataSource = dtEmployee;
    gvDemo.DataBind();
}
protected DataTable PopulateDataTable()
{ 
    //here is logic to populate your data table
    //I'm using dummy data
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
    dt.Columns.Add(new DataColumn("LastName", typeof(string)));

    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());

    dt.Rows[0]["FirstName"] = "Sangram";
    dt.Rows[0]["LastName"] = "Patil";

    dt.Rows[1]["FirstName"] = "Prasad";
    dt.Rows[1]["LastName"] = "Patil";

    dt.Rows[2]["FirstName"] = "Girish";
    dt.Rows[2]["LastName"] = "Mane";

    dt.Rows[3]["FirstName"] = "Kedar";
    dt.Rows[3]["LastName"] = "Torase";

    dt.Rows[4]["FirstName"] = "Nandu";
    dt.Rows[4]["LastName"] = "Desai";

    dt.Rows[5]["FirstName"] = "Promod";
    dt.Rows[5]["LastName"] = "Mandavkar";

    dt.Rows[6]["FirstName"] = "Kiran";
    dt.Rows[6]["LastName"] = "More";

    dt.Rows[7]["FirstName"] = "Vaibhav";
    dt.Rows[7]["LastName"] = "Telang";

    return dt;
}
Sign up to request clarification or add additional context in comments.

1 Comment

this worked well, but I did change the GetElementById to look for a static name, ContextId did not work for me. this sovled my issue so I could focus on the business logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.