Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have a textbox and gridview when i write some value in textbox ,its corresponding records will be shown in the Gridview.


Thanks
Posted
Comments
Prerak Patel 2-Sep-11 6:55am    
That is good. So, what is the problem?

onblur of your text box you can make an jQuery.ajax call to fetch the data from the server side and then you can render those data in GridView.

If you want then you can give some timer as well say after onblur give 5 sec then fetch the record and refresh the gridview.
 
Share this answer
 
Comments
3796068 2-Sep-11 13:13pm    
Dear sir,
Can you please give some code.

Thanks
search.aspx
JavaScript
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="text" id="txtSearch" /> 
    </div>
    <div id="gridPanel">
    </div>
    </form>
</body>
</html>

</html>

JavaScript
//used jquery plz put the reference of jquery file
//Search method to search , bind grid with data, and load the grid page.
function search (txt){
    //getting search Parameters as an JSON object.
    var searchParams = {"Name" : txt};
    //calling HTTP post ajax to load the gridview page in div.
    $("#gridPanel").load("gridview.aspx",searchParams);
}
$(function ( ) {
    // binding blur event. 
    $("#txtSearch").blur(function (e) {
        var txt = this.value;
        // calling search method after 2 seconds of 
        // entering Text on search   Textbox.
        setTimeout(function ( ) {
            search(txt)
        }, 2000);
    });
});

gridview.aspx
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:gridview id="GridView1" autogeneratecolumns="true" runat="server" xmlns:asp="#unknown">
        </asp:gridview>
    </div>
    </form>
</body>
</html>


Gridview.aspx.cs

C#
public partial class gridview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Request.Form["name"].ToString();
        //search on the basis of name , get the datatable / dataset 
        //assign to datasource of the grid. Then bind the grid.
        GridView1.DataSource = DataBase.getData(name);
        GridView1.DataBind();
    }
}
public class DataBase
{
    private static DataTable getSchema()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        dt.Columns.Add(new DataColumn("Address", typeof(string)));
        return dt;
    }
    private static DataTable getData()
    {
        DataTable dt = getSchema();

        DataRow dr = dt.NewRow();
        dr[0] = "Rupesh";
        dr[1] = "Brjn";
        dt.Rows.Add(dr);

        DataRow dr1 = dt.NewRow();
        dr1[0] = "Ritesh";
        dr1[1] = "Jsg";
        dt.Rows.Add(dr1);
        return dt;
    }
    public static DataTable getData(string name)
    {
       
        DataTable dt = getData();
        DataRow[] drs = dt.Select("Name='" + name + "'");
        DataTable dtResult = getSchema();
        foreach (DataRow dr in drs)
        {
            DataRow drNew = dtResult.NewRow();
            drNew.ItemArray = dr.ItemArray;
            dtResult.Rows.Add(drNew);
        }
        return dtResult;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900