Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
How to filter gridview date on textbox keypress in asp.net ?
Posted 21-Feb-13 0:40am
Comments
thevivek.pro at 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
Tadit Dash at 21-Feb-13 6:55am
   
If this is an answer, then please add in answer box and delete from here.
Sandeep Mewara at 21-Feb-13 11:06am
   
What have you tried so faR?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 434
1 Maciej Los 155
2 Richard MacCutchan 130
3 DamithSL 129
4 Kornfeld Eliyahu Peter 119


Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid