ASP
  Home arrow ASP arrow Page 2 - Real Time Data Grid Part 2/2
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

Real Time Data Grid Part 2/2
By: Annette Tennison
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    2002-02-08

    Table of Contents:
  • Real Time Data Grid Part 2/2
  • The JavaScript code
  • The JavaScript code (contd.)
  • The doDel JavaScript function
  • Using our DynamicGrid class
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Real Time Data Grid Part 2/2 - The JavaScript code


    (Page 2 of 6 )

    The code that handles the updating and deleting of records "behind the scenes" is written in JavaScript and is output to the browser at the beginning of the DisplayGrid routine. Let's take a look at that code now.

    When the update button next to each field is clicked, the doUpdate JavaScript function is called. Its signature looks like this:

    function doUpdate(recordId, fieldId)

    It accepts the ID of the record to update, as well as the ID of the field to update. If you're wondering where the heck we get these values from when the update button is clicked, then take a look at the code that's uses to actually output the text box and update button to the browser:

    <input id="grid_<%=intRecordCounter%>_<%=intFieldCounter%>" type="text" name="<%=objField.Name%>" value="<%=objField.Value%>" style="width:75%">

    <input type="button" value="Update" onClick="doUpdate(<%=intRecordCounter%>, <%=intFieldCounter%>)">


    The intRecordCounter variable is a counter that's incremented when we call the MoveNext method of the recordset. The intFieldCounter variable is updated as we display each field in each record of the result set returned. The actual values being passed to the doUpdate JavaScript function mean nothing unless we can use them to get both the name and value of the field that we want to change:

    strFieldName = eval("grid_"+recordId+"_"+fieldId+".name");

    strFieldValue = eval("grid_"+recordId+"_"+fieldId+".value");

    strPrimaryFieldName = eval("grid_"+recordId+"_0.name");

    strPrimaryFieldValue = eval("grid_"+recordId+"_0.value");


    We use the eval JavaScript function to get the name and value of the field that we are updating. The eval function takes one parameter that is a JavaScript command. It evaluates that command and returns the result. In the code snippet above, we use the eval function to get the name and value of the field we're updating. We also use it to get the name and value of the first field in that particular record.

    strCriteria = strFieldName + " = '" + strFieldValue + "'";

    On the line above, we are using the field name and its new value to build a string variable. This variable will be used in the SQL query we're about to build:

    strQuery = "UPDATE <%=m_TableName%> SET "+strCriteria+" WHERE "+strPrimaryFieldName+" = '"+ReplaceAllQuotes(strPrimaryFieldValue)+"'";

    In the line above, we build an SQL query that will be passed to our server using XMLHTTP. It is an update query, because we're updating a record. Notice how we can use ASP code even within a JavaScript function to pass in the name of the table we are working with? Here's how the strQuery variable looked when I chose to update the fname field of the employee tables in the pubs database:

    UPDATE employee SET fname='Michael' WHERE emp_id='PMA42628M'

    See how the where clause needs a unique field to differentiate between the records that it's updating? There's one catch to using our DynamicGrid class: the first field of the table you're working with MUST be a uniquely identified primary key. This isn't really a problem however, because most tables are already setup this way.

    blnSuccess = SendRemoteRequest(strQuery);

    Next, we call the SendRemoteRequest JavaScript function, which uses XMLHTTP to send our SQL query to the server. Let's look at the SendRemoteRequest function now:

    function SendRemoteRequest(query)

    {

    var objXMLHTTP = new ActiveXObject("MSXML2.XMLHTTP");

    var strPage = new String();



    strPage = "dynamic.datagrid.asp?query="+query;

    objXMLHTTP.Open("GET", strPage, 0);

    objXMLHTTP.Send();

    return objXMLHTTP.responseText;

    }

    More ASP Articles
    More By Annette Tennison


     

    ASP ARTICLES

    - Central Scoreboard with Flash and ASP
    - Calorie Counter Using WAP and ASP
    - Creating PGP-Encrypted E-Mails Using ASP
    - Be My Guest in ASP
    - Session Replacement in ASP
    - Securing ASP Data Access Credentials Using t...
    - The Not So Ordinary Address Book
    - Adding and Displaying Data Easily via ASP an...
    - Sending Email From a Form in ASP
    - Adding Member Services in ASP
    - Removing Unconfirmed Members
    - Trapping HTTP 500.100 - Internal Server Error
    - So Many Rows, So Little Time! - Case Study
    - XDO: An XML Engine Class for Classic ASP
    - Credit Card Fraud Prevention Using ASP and C...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek