Click here to Skip to main content
6,822,613 members and growing! (17,075 online)
Email Password   helpLost your password?
Database » Database » General     Advanced

A db grid control using ASP

By Bjornar Henden

A grid control for ASP written in JavaScript to display recordsets.
Windows, ASP, IIS, Dev
Posted:12 May 2001
Updated:27 Aug 2001
Views:285,775
Bookmarked:85 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
48 votes for this article.
Popularity: 7.75 Rating: 4.61 out of 5

1

2
1 vote, 3.2%
3
10 votes, 32.3%
4
20 votes, 64.5%
5

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.

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

<%@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:

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

About the Author

Bjornar Henden


Member

Occupation: Web Developer
Location: Norway Norway

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 96 (Total in Forum: 96) (Refresh)FirstPrevNext
Question'adUseClient' is undefined Pinmembertoota17:47 16 Jul '08  
AnswerRe: 'adUseClient' is undefined Pinmembercocchio0:58 18 Sep '08  
General.net is better Pinmembershakirhussain21:30 18 Nov '07  
QuestionError PinmemberRITIK DODHIWALA21:32 29 Apr '07  
Questionvisual web developper [modified] PinmemberMoukala0:59 15 Jun '06  
GeneralInclude the datetime var into DispalyName Pinmemberellaz7:16 2 May '06  
GeneralRe: Include the datetime var into DispalyName Pinmemberellaz11:30 2 May '06  
QuestionWorks great, but cannot save changes Pinmemberazharmahmood19:13 22 Oct '05  
AnswerRe: Works great, but cannot save changes Pinmemberturist6911:17 5 Feb '07  
QuestionPlease give me a way for display pictures which store in DB Access 2003 by "OLE object"? Pinmembertuyenhnp17:44 25 Sep '05  
GeneraldbGrid PinmemberKuy Tan6:41 1 Sep '05  
GeneralCan't edit using SQL SERVER PinmemberTracer_II3:10 9 Aug '05  
QuestionRe: Can't edit using SQL SERVER Pinmemberazharmahmood3:16 23 Oct '05  
AnswerRe: Can't edit using SQL SERVER PinsussAnonymous23:29 24 Oct '05  
QuestionRe: Can't edit using SQL SERVER PinmemberGiliDazzle21:32 14 Mar '07  
QuestionRe: Can't edit using SQL SERVER Pinmemberozbarney14:45 18 Nov '05  
QuestionRe: Can't edit using SQL SERVER Pinmemberemmpergul15:49 14 Nov '06  
AnswerRe: Can't edit using SQL SERVER PinmemberWill662:02 12 Jun '07  
GeneralGood job..does this work with non-IE browsers Pinmemberrra3034:30 7 Aug '05  
GeneralASP Chart PinmemberMajd Ghiba22:05 21 Mar '05  
GeneralRe: ASP Chart Pinmembermintyboy692:24 4 May '05  
If you search around there is a peice of ASP code and a flash file that displays a bar chart, cant remeber the project name but I use it for a management stat page on our intranet.

Big Grin
GeneralRe: ASP Chart PinmemberGreen Eyes2:28 4 May '05  
GeneralNice Job Pinmemberjhidey8:30 23 Dec '04  
General[Message Deleted] Pinmembernanpal763:05 18 Nov '04  
GeneralNot Saving ..please help PinmemberGiliDazzle22:16 14 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 27 Aug 2001
Editor: Chris Maunder
Copyright 2001 by Bjornar Henden
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project