Click here to Skip to main content
13,257,667 members (53,189 online)
Click here to Skip to main content
Add your own
alternative version


88 bookmarked
Posted 12 May 2001

A db grid control using ASP

, 27 Aug 2001
Rate this:
Please Sign up or sign in to vote.
A grid control for ASP written in JavaScript to display recordsets.
<!-- Download Links --> <!-- Article image -->

Sample Image - bgrid1.gif

<!-- Add the rest of your HTML here -->


A grid control for editing databases on the web. It is written for ASP with JavaScript, but should be fully usable from VBScript as well. With just a few lines of code, you will, with this control, be able to edit your data.


  • Write protect fields you don't want the user to edit.
  • ID fields can look up values in other recordsets.
  • Sort by any column that supports sorting.
  • Paging with custom page size.
  • Automatically detects primary key, but can also be manually overridden.
  • Get default values for new records with custom select statement.


To use the grid you create it by calling new BGrid(rs) from JavaScript, or you can alternately create it by calling CreateGrid(rs) which also works from VBScript. The parameter in both methods is the recordset you want the grid to display. This recordset should allow moving both back and forward, so I usually set CursorLocation to adUseClient on my connection.

I'm too lazy to manually declare all those ad-constants, so at the top of config.asa I add the following. This means that all ADO constants are available, and is required for the demo to work without a lot of work.

<!--<span class="code-comment"> METADATA NAME="Microsoft ActiveX Data Objects 2.5 Library"
              TYPE="TypeLib" UUID="{00000205-0000-0010-8000-00AA006D2EA4}" --></span>

A simple example

Using the grid requires only a few lines of code:


<!--#include file=misc.asp-->

<!--#include file=b.dropdown.asp-->

<!--#include file=b.grid.asp-->

<form method=post>


var Conn = CreateConnection(); // CreateConnection is defined in misc.asp

var rs = Server.CreateObject("ADODB.Recordset");


var Grid = new BGrid(rs);

Grid.Process(); // Process must be called to handle saving and such stuff

Grid.Display(); // Display does just that

Grid = null;


rs = null;


Conn = null;


Since the EmployeeTerritories table has a field called TerritoryID which is a foreign key to the Territories table, we can tell the grid to look for a value to display from this table instead of displaying an ID that means nothing:

var rsTerritories = Conn.Execute("Territories");

When a record is being edited it will look similar to this picture:



  • CreateGrid(recordset)
  • Grid.Process()
  • Grid.Display()
  • Grid.SetDefault(field,sql)
  • Grid.ProtectFields(fieldlist)
  • Grid.SetLookup(fkfield,recordset,pkfield,displayfield)
  • Grid.SetOption(option,value)

Some of these are explained above, but there are a few I haven't mentioned yet:


option can be one of the following: "debug", "pagesize", "pk", "truncate". value varies depending on the option. "debug" requires a boolean, "pagesize" requires a number, "pk" is a comma separated list of fields which is the manually overridden primary key, and "truncate" needs a number to know how many characters that should be displayed of every field in the grid. Some examples:

Grid.SetOption("debug",true);           // default is false
Grid.SetOption("pagesize",20);          // default is 10
Grid.SetOption("pk","field1,field2");   // if the primary key can't be found
Grid.SetOption("truncate",30);          // Don't display more than 30 characters


field is the name of the field in the recordset, and sql is the sql-statement used to find the default value for new records in this grid.

Grid.SetDefault("UserID","select max(UserID)+1 from TheTable"); // NOT the way to do it,
                                                                // but it shows the usage.
Grid.SetDefault("HireDate","select getdate()");                 // This was better perhaps.
Grid.SetDefault("MinAge","select 18");                          // A constant.


Protects the fields in the fieldlist from editing. Example:

Grid.ProtectFields("UserID,HireDate");  // UserID and HireDate can't be edited.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Bjornar Henden
Web Developer
Norway Norway
No Biography provided

You may also be interested in...


Comments and Discussions

Question'adUseClient' is undefined Pin
toota16-Jul-08 17:47
membertoota16-Jul-08 17:47 
AnswerRe: 'adUseClient' is undefined Pin
cocchio18-Sep-08 0:58
membercocchio18-Sep-08 0:58 is better Pin
shakirhussain18-Nov-07 21:30
membershakirhussain18-Nov-07 21:30 
QuestionError Pin
RITIK DODHIWALA29-Apr-07 21:32
memberRITIK DODHIWALA29-Apr-07 21:32 
Questionvisual web developper [modified] Pin
Moukala15-Jun-06 0:59
memberMoukala15-Jun-06 0:59 
GeneralInclude the datetime var into DispalyName Pin
ellaz2-May-06 7:16
memberellaz2-May-06 7:16 
GeneralRe: Include the datetime var into DispalyName Pin
ellaz2-May-06 11:30
memberellaz2-May-06 11:30 
QuestionWorks great, but cannot save changes Pin
azharmahmood22-Oct-05 19:13
memberazharmahmood22-Oct-05 19:13 
AnswerRe: Works great, but cannot save changes Pin
turist695-Feb-07 11:17
memberturist695-Feb-07 11:17 
QuestionPlease give me a way for display pictures which store in DB Access 2003 by &quot;OLE object&quot;? Pin
tuyenhnp25-Sep-05 17:44
membertuyenhnp25-Sep-05 17:44 
GeneraldbGrid Pin
Kuy Tan1-Sep-05 6:41
memberKuy Tan1-Sep-05 6:41 
GeneralCan't edit using SQL SERVER Pin
Tracer_II9-Aug-05 3:10
memberTracer_II9-Aug-05 3:10 
QuestionRe: Can't edit using SQL SERVER Pin
azharmahmood23-Oct-05 3:16
memberazharmahmood23-Oct-05 3:16 
AnswerRe: Can't edit using SQL SERVER Pin
Anonymous24-Oct-05 23:29
sussAnonymous24-Oct-05 23:29 
QuestionRe: Can't edit using SQL SERVER Pin
GiliDazzle14-Mar-07 21:32
memberGiliDazzle14-Mar-07 21:32 
QuestionRe: Can't edit using SQL SERVER Pin
ozbarney18-Nov-05 14:45
memberozbarney18-Nov-05 14:45 
Have you found a solution for this yet???
My connection string is:
Conn.Open("Driver={SQL Server};Server=My_Server;Database=NorthWind;Uid=xx;Pwd=xx;")
Everything works fine, but when the edit window comes up, none of the fields are selectable ??
QuestionRe: Can't edit using SQL SERVER Pin
emmpergul14-Nov-06 15:49
memberemmpergul14-Nov-06 15:49 
AnswerRe: Can't edit using SQL SERVER Pin
Will6612-Jun-07 2:02
memberWill6612-Jun-07 2:02 
GeneralRe: Can't edit using SQL SERVER Pin
Member 82798784-Oct-11 12:19
memberMember 82798784-Oct-11 12:19 
GeneralGood job..does this work with non-IE browsers Pin
rra3037-Aug-05 4:30
memberrra3037-Aug-05 4:30 
GeneralASP Chart Pin
Majd Ghiba21-Mar-05 22:05
memberMajd Ghiba21-Mar-05 22:05 
GeneralRe: ASP Chart Pin
mintyboy694-May-05 2:24
membermintyboy694-May-05 2:24 
GeneralRe: ASP Chart Pin
Green Eyes4-May-05 2:28
memberGreen Eyes4-May-05 2:28 
GeneralNice Job Pin
jhidey23-Dec-04 8:30
memberjhidey23-Dec-04 8:30 
General[Message Deleted] Pin
nanpal7618-Nov-04 3:05
membernanpal7618-Nov-04 3:05 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 28 Aug 2001
Article Copyright 2001 by Bjornar Henden
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid