Click here to Skip to main content
Click here to Skip to main content

A db grid control using ASP

By , 27 Aug 2001
 

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
Web Developer
Norway Norway
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Question'adUseClient' is undefinedmembertoota16 Jul '08 - 16:47 
Hi, I want to edit values of four columns from a table only. Table contains 15 columns. i want to do this using data grid in ASP only because i know ASP little bit .
I download this code and when i run the demo files it returns an error saying
 
Microsoft JScript runtime error '800a1391'
 
'adUseClient' is undefined
 
/datagrid/demo/misc.asp, line 35
 
I dont know how to solve this. Please let me know how to fix this problem.
 
Thanks and Regards
AnswerRe: 'adUseClient' is undefinedmembercocchio17 Sep '08 - 23:58 
Phs problems with ADO constants.
if you dont want to modify global.asa inside the server root, adding, at the first line, :
 
<!-- METADATA NAME="Microsoft ActiveX Data Objects 2.5 Library"
TYPE="TypeLib" UUID="{00000205-0000-0010-8000-00AA006D2EA4}" -->
 
you can include, before "misc.asp" inclusion, the file: "adojavas.inc" which provide ADO const for JAVASCRIPT.
 
Adojavas.inc is in your pc: c: \Program Files\Common Files\System\ado\folder.
General.net is bettermembershakirhussain18 Nov '07 - 20:30 
Great work , But now days ppls going to .net there is already almost database controlls are available l
 

QuestionErrormemberRITIK DODHIWALA29 Apr '07 - 20:32 
Hi frineds,
I m trying to pass Query string in place of tbl in rs.open statement and i replaces adcmdtable to adcmd text. but it does not accepts. gives run time error.
 
plz help

Questionvisual web developper [modified]memberMoukala14 Jun '06 - 23:59 
hi there
 
any idea on how to use this great control in visual web dev?
 
thanks
 
-- modified at 10:27 Thursday 15th June, 2006
GeneralInclude the datetime var into DispalyNamememberellaz2 May '06 - 6:16 
Hi,
 
I am going to use your code. It works perfect and looks so friendly. Thank you very much!
 
Could you please help me to modify it?
I would like to add the date/numbers variables as part of DispalyName of the SQL query.
 
For example I would like to see the employee's birth date (DEMO, employeeterritories.asp code).
 
To do it I modified your code as:
 
var rsEmployee = Conn.Execute("select EmployeeID, LastName + ', ' + CAST(BirthDate as varchar(10)) as DisplayName from Employee order by LastName");
 
The error message was: ODBC Microsoft Access Driver Syntax error (missing operator) in query expression 'LastName + ', ' + CAST(BirthDate as varchar(10))'.
 
Any idea what did I miss? I don't have much experience with ASP. Please help.
 

 
Best regards,
Ella
GeneralRe: Include the datetime var into DispalyNamememberellaz2 May '06 - 10:30 
I found it myself.
CAST() function doesn't work when you work with Access. it is just for SQL server.
I used CSTR() and now it is working
 

 
Best,
Ella
 
-- modified at 16:31 Tuesday 2nd May, 2006
QuestionWorks great, but cannot save changesmemberazharmahmood22 Oct '05 - 18:13 
I am using your control, great job, I have no problems except one
 
Error saving record: [Microsoft][ODBC Microsoft Access Driver] Operation must use an updateable query.
 
I even tried to use oledb connection string, no luck. Please advise.
Thanks
Azhar
azhar@edusoftusa.com
Roll eyes | :rolleyes:
AnswerRe: Works great, but cannot save changesmemberturist695 Feb '07 - 10:17 
i have the same issue... any suggestions?
 
Thanks
QuestionPlease give me a way for display pictures which store in DB Access 2003 by &quot;OLE object&quot;?membertuyenhnp25 Sep '05 - 16:44 
Hi all!
I store my picture in Access 2003 by "OLE Object".
I am coding a ASP page for loading and display those pictures. But I am not know any way which display those pictures in my ASP page.
Please, give me a way. Thanks very much.

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

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