Click here to Skip to main content
15,886,728 members
Articles / Web Development / ASP.NET
Article

Filterable Grid for ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.67/5 (6 votes)
12 Aug 2004GPL3 81.9K   1.5K   42   6
An article on a filterable Grid for ASP.NET.

Sample Image

Introduction

This article presents an ASP.NET DataGrid with ability to filter based on column values.

Details

I tried to keep the code simple. This code uses JavaScript method filtergrid to build QueryString for the URL. The datasource is filtered based on QueryString values.

JavaScript
 // this method sets the filter condition.
function filtergrid(columnname)
{
  var val;
  var baseurl;
  
  // I have set the baseurl of the demo page here.
  // You can simply replace it with your .aspx page name
  // or may be read programmatically
  // to make this control generic.
  baseurl = 'FilterGridPage.aspx';
  val = document.getElementById('txt' + columnname).value;
  
  self.location.href = baseurl + '?selectedcolumn=' 
                   + columnname + '&selectedvalue=' + val;
}

In the code-behind file, the FillDataGridColumns method creates the column headers with HTML textbox and button.

VB
'// header cell code
headerhtml = DisplayNames(i)

'// create html text
headerhtml = headerhtml & " <br> " & " & _
"<Input type=text class=FilterTextBox id=txt" & ColumnNames(i) & " /> " & _
"<Input class=ButtonStyle type=button id=btn" & ColumnNames(i) & _
" onclick=""javascript:filtergrid('" & ColumnNames(i) & "');"" value='Go'/>"

'// simply set the html text as HeaderText property of BoundColumn
<BoundColumnVariable>.HeaderText = headerhtml

Scope of further development

  • At present, this control handles String and Double datatypes only. It can be modified to handle other datatypes.
  • Filter works on single value only and can not search for multiple values.

All your suggestions and comments are welcome.

Conclusion

The use of basic JavaScript and code-behind file helps to enhance the functionality of the DataGrid control.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Founder Absolute Agile
Australia Australia
Hemang Shukla is Founder of Absolute Agile. Absolute Agile is a Melbourne based Agile training and consulting firm. http://www.absoluteagile.com.au

Hemang's blogs are also available at http://www.hemangshukla.blogspot.com/

Comments and Discussions

 
Generalthe baseurl Pin
zxd991513-Oct-08 17:01
zxd991513-Oct-08 17:01 
GeneralA More Generic method Pin
chuck the coder12-Apr-05 1:35
chuck the coder12-Apr-05 1:35 
GeneralRe: A More Generic method Pin
kvc23-Apr-05 9:57
kvc23-Apr-05 9:57 
GeneralRe: A More Generic method Pin
chuck the coder26-Apr-05 4:47
chuck the coder26-Apr-05 4:47 
GeneralEfficient filter !!!!! Pin
Sukim21-Aug-04 7:59
Sukim21-Aug-04 7:59 
GeneralRemove filter Pin
Sebastien Lorion18-Aug-04 12:49
Sebastien Lorion18-Aug-04 12:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.