Read cell values from datagrid/gridview using client-side vbscript in web app

Posted on 2007-07-20
Last Modified: 2008-01-09
I have created aweb application in ASP.NET 2.0 with VB.NET (VS2005) and SQL Server 2005. I have Word 2003 templates that must be filled through automation on the client-side (VBScript) - each user's location has indvidualized templates. I was hoping to be able to fill the Word doc from either a datagrid or gridview which is made up of a single row. I have a button on the page with the datagrid/gridview that triggers the VBScript, opening the bookmarked Word template. My probem is how do I read each cell from the datagrid/gridview.

The relevant code for a DataGrid is:
<form id="form1" runat="server">
            <asp:datagrid id="DataGrid1" runat="server" GridLines="Vertical" CellPadding="3" BackColor="White"
                BorderColor="#999999" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%" Font-Size="X-Small"
                  <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
                  <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
                  <ItemStyle BorderWidth="2px" ForeColor="Black" BorderStyle="Solid" BorderColor="Black" BackColor="#EEEEEE"></ItemStyle>
                  <HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px" ForeColor="White" BorderStyle="Solid"
                  BorderColor="Black" BackColor="#000084"></HeaderStyle>
                  <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
                  <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
            <asp:TextBox ID="WPAID" runat="server" Text="-1391708654"></asp:TextBox>
            <asp:Button id="Button1" usesubmitbehavior="true" Text="Print WPA" OnClientClick="FillWPA()" runat="server" />
<script type="text/vbscript">

        function FillWPA()
            Dim oWord, oDoc, oConn, sCommand, sReader, oGridItem
            Set oWord = CreateObject("Word.Application")
            Set oDoc = oWord.Documents.Open("C:\NM_Works_All\HSD\Templates\", , False)
            oWord.Visible = true

            oDoc.FormFields("Date").result = document.form1("DataGrid1").Item.Rows(0).Cells(2).Value
            oDoc.FormFields("Geo").result = document.form1("DataGrid1").Item.Rows(0).Cells(3).Value
            oDoc.FormFields("Adm").result = document.form1("DataGrid1").Item.Rows(0).Cells(4).Value
            oDoc.FormFields("Edu").result = document.form1("DataGrid1").Item.Rows(0).Cells(100).Value
            oDoc.SaveAs "C:\NM_Works\Documents\WPA_Test.doc"

I get an error: Microsoft VBScript runtime error: Object required: 'document.form1(...)'

          end function
Question by:wildonthefly
    LVL 10

    Expert Comment

    try using document.forms(0) instead of document.form1

    Author Comment

    New error: Microsoft VBScript runtime error: Object doesn't support this property or method: 'document.forms(...).Item.Rows'

    In your solution, where do identify DataGrid1?
    LVL 58

    Accepted Solution

    Dim oDataGrid

    oDataGrid = document.getElementById("<%= DataGrid1.ClientID %>")

    'Now you have instance of Table that DataGrid produces on the client side in variable oDataGrid
    'You could use

    FirstCellInFirstRow = oDataGrid.rows(0).cells(0).innerHTML

    and so on. Keep in mind that depending upon the ItemTemplate you may have to go even deeper in cell to get exact value. Although I see that you probably are not using any.

    Author Comment

    Thanks for speedy response.
    Only one change; it needed Set:
    Set oDataGrid = document.getElementById("<%= DataGrid1.ClientID %>")
    LVL 58

    Expert Comment

    Oh yes. I am used to Javascript on the client side :)

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    This video discusses moving either the default database or any database to a new volume.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now