Click here to Skip to main content
15,886,734 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to filter gridview date on textbox keypress in asp.net ?
Posted
Comments
thevivek.pro 21-Feb-13 6:50am    
After Binding a grid you can filter grid using this code..



protected void btnFiltering_Click( object sender, EventArgs e)

{ // Filtering of Data using DataView

string strConn = "Data Source=localhost;Initial Catalog= pubs; user Id=sa;Password =test" ;

string sqlQuery = "select * from stores" ;

SqlConnection conn = new SqlConnection (strConn);

SqlDataAdapter ada = new SqlDataAdapter (sqlQuery, conn);

DataTable dtStore = new DataTable ();

ada.Fill(dtStore);

//Create DataView Object

DataView dvStores = new DataView (dtStore);

dvStores.RowFilter = "state = 'CA'" ;

//Bind GridView : Only 'CA' state

grdvFilter.DataSource = dvStores;

grdvFilter.DataBind();

}

protected void btnSorting_Click( object sender, EventArgs e)

{ //Sorting of Data Using DataView

string strConn = "Data Source=localhost;Initial Catalog= pubs; user Id=sa;Password = test" ;

string sqlQuery = "select * from stores" ;

SqlConnection conn = new SqlConnection (strConn);

SqlDataAdapter ada = new SqlDataAdapter (sqlQuery, conn);

DataTable dtStore = new DataTable ();

ada.Fill(dtStore);

//Create DataView Object

DataView dvStores = new DataView (dtStore);

dvStores.Sort = "city" ;

//Bind GridView : Sorting By City

grdvSort.DataSource = dvStores;

grdvSort.DataBind();

}

}

If any query regarding this feel free to ask me.
vivek mehta thevivek.pro
If this is an answer, then please add in answer box and delete from here.
Sandeep Mewara 21-Feb-13 11:06am    
What have you tried so faR?

XML
You can do it in multiple ways. If you want to use your existing code, you can do it this way

<script type="text/javascript">
    function refreshData() {
        document.getElementById('<%= YourButtonID.ClientID %>').click();
    }
</script>

<asp:TextBox ID="TextBox1" runat="server" onkeypress="refreshData();"></asp:TextBox>
 
Share this answer
 
Though GridView is server side control so i suggest you re consider your requirement from client side keypress event to server side TextChanged event. If it can possible then you add OnTextChanged event handler to textbox with set AutoPostback property true. From serverside TextChangedEvent handler you can filter your grid view. Remember when you want to switch the text box that time the server event will fire. The benefit is that you can not do anything from client side(JavaScript).
If it is not possible then next my suggestion is you go for ajax request and instead of GridView serverside control, you manually render html table and update it by ajax call from client side with javascript keypress event. Think like ASP.NET MVC there are no any server side control (gridview,datalist etc), that technology, all need to do manually using html/html helpers.
 
Share this answer
 
v2

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