Click here to Skip to main content
15,880,796 members
Articles / Web Development / IIS
Article

A db grid control using ASP

Rate me:
Please Sign up or sign in to vote.
4.74/5 (34 votes)
27 Aug 20012 min read 523.8K   11K   88   98
A grid control for ASP written in JavaScript to display recordsets.

Sample Image - bgrid1.gif

Introduction

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.

Features

  • 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.

Usage

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.

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

A simple example

Using the grid requires only a few lines of code:

ASP
<%@Language="JavaScript%">
<!--#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");
rs.Open("EmployeeTerritories",Conn,adOpenStatic,adLockOptimistic,adCmdTable);

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.Close();
rs = null;
Conn.Close();
Conn = null;
%>
</form>

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:

JavaScript
var rsTerritories = Conn.Execute("Territories");
Grid.SetLookup("TerritoryID",rsTerritories,"TerritoryID","TerritoryDescription");

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

Editing

Functions

  • 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:

Grid.SetOption(option,value)

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

Grid.SetDefault(field,sql)

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.

Grid.ProtectFields(fieldlist)

Protects the fields in the fieldlist from editing. Example:

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

License

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


Written By
Web Developer
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: How to update many records on many pages? Pin
Bjornar Henden16-Oct-02 8:26
Bjornar Henden16-Oct-02 8:26 
QuestionHow to update many records on many pages? Pin
Anonymous15-Oct-02 21:14
Anonymous15-Oct-02 21:14 
GeneralBroken Links Pin
Anonymous13-Oct-02 3:43
Anonymous13-Oct-02 3:43 
GeneralConnection to SQL Server results in read only fields Pin
JCC13-Jun-02 3:54
JCC13-Jun-02 3:54 
GeneralRe: Connection to SQL Server results in read only fields Pin
18-Jun-02 6:43
suss18-Jun-02 6:43 
GeneralRe: Connection to SQL Server results in read only fields Pin
Kuy Tan31-Aug-05 9:37
Kuy Tan31-Aug-05 9:37 
GeneralRe: Connection to SQL Server results in read only fields Pin
Member 143566920-Nov-04 1:52
Member 143566920-Nov-04 1:52 
GeneralConnect to Oracle - textboxes read only Pin
29-May-02 4:15
suss29-May-02 4:15 
GeneralRe: Connect to Oracle - textboxes read only Pin
4-Jun-02 7:41
suss4-Jun-02 7:41 
Questionhow to add record to sql server 2000 db usin javascript in asp page Pin
16-May-02 2:23
suss16-May-02 2:23 
GeneralAbout Function BGrid(rs,pk) Pin
10-Apr-02 10:34
suss10-Apr-02 10:34 
Generalnot working at all Pin
23-Feb-02 6:13
suss23-Feb-02 6:13 
Generaledittable.asp does not work Pin
JCC15-Feb-02 8:32
JCC15-Feb-02 8:32 
GeneralGot to see yet another more superb Grid Pin
13-Nov-01 7:26
suss13-Nov-01 7:26 
GeneralRe: Got to see yet another more superb Grid Pin
2-Dec-01 10:04
suss2-Dec-01 10:04 
GeneralRe: Got to see yet another more superb Grid Pin
27-May-02 16:06
suss27-May-02 16:06 
GeneralORACLE NOT FUCTION!!! Pin
28-Oct-01 22:33
suss28-Oct-01 22:33 
Generaladd reccord Pin
24-Oct-01 1:06
suss24-Oct-01 1:06 
Generaladding the ADO constants decleration Pin
12-Oct-01 13:33
suss12-Oct-01 13:33 
GeneralRe: adding the ADO constants decleration Pin
guille13-Oct-02 3:24
guille13-Oct-02 3:24 
Generalhelp, the demo doesn't work Pin
mcaycedo7-Oct-01 12:35
mcaycedo7-Oct-01 12:35 
GeneralRe: help, the demo doesn't work Pin
9-Nov-01 2:01
suss9-Nov-01 2:01 
GeneralRe: help, the demo doesn't work Pin
9-Nov-01 2:02
suss9-Nov-01 2:02 
GeneralRe: help, the demo doesn't work Pin
Narayana Katari14-Nov-02 0:04
Narayana Katari14-Nov-02 0:04 
GeneralGood job - two issues Pin
J. Diamond9-Sep-01 2:44
J. Diamond9-Sep-01 2:44 

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.