Asynchronously Populating Calculated Columns in a GridView

Last post 11-11-2009 12:54 PM by mayuresh_s. 2 replies.

Sort Posts:

  • Asynchronously Populating Calculated Columns in a GridView

    03-18-2007, 12:05 PM
    • Member
      459 point Member
    • mtsonic
    • Member since 05-22-2006, 6:37 PM
    • Posts 217

    My goal here is to remove some slow-running functions from my sql (which are causing the page/query to time out) and have them asynchronously populate after the main data of the page has loaded and is displayed.  I've watched this video

    http://asp.net/learn/videos/view.aspx?tabid=63&id=118
    How Do I: Implement the AJAX Incremental Page Display Pattern?

     and tried to implament something but I get no results and no errors.  Meaning the text that is suppose to come back from the web service is not.  Here is what I have.

    1) I programmatically create a div tag in the column with a unique id and set the RowCreated handler to fire the javascript.  I could not put it on the RowDataBound because the step below uses it.

                If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Selected Then

                    Dim engineeringDocumentId As String = dataRow.Item("EngineeringDocumentId").ToString

                    Dim rowClientId As String = e.Row.ClientID

                    e.Row.Cells(5).Text = "<DIV style=" & Chr(34) & "WIDTH: 150px; HEIGHT: 20px; BACKGROUND-COLOR: palegreen" & Chr(34) & " id=" & Chr(34) & rowClientId & "RevisionStatus" & Chr(34) & "></DIV>"

                    CType(e.Row.NamingContainer, GridView).Attributes.Add("RowCreated", "return GetRevisionStatus('" & engineeringDocumentId & "', '" & rowClientId & "RevisionStatus');")

                End If

    2)  Here's the javascript with the ScriptManagerProxy

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

        <asp:ScriptManagerProxy ID="EngineeringDocumentsSM" runat="server">

            <services>

                <asp:ServiceReference Path="~/EngineeringDocuments/EngineeringDocumentsWebService.asmx" />

            </services>

        </asp:ScriptManagerProxy>

        <script language="javascript" type="text/javascript">

        <!--

        function GetRevisionStatus(engineeringDocumentId, htmlElementId) {

        ret = EngineeringDocumentsWebService.GetRevisionStatus(engineeringDocumentId, htmlElementId, OnGetRevisionStatusComplete(htmlElementId), OnGetRevisionStatusTimeOut, OnGetRevisionStatusError);

        }

       

        function OnGetRevisionStatusComplete(htmlElementId, result) {

        document.getElementById(htmlElementId).innerHTML = result;

        }

       

        function  OnGetRevisionStatusTimeOut(results) {

        alert("TIMEOUT");

        }

           

        function OnGetRevisionStatusError(result) {

        alert("ERROR");

        }

        // -->

        </script>

    3)  Finally here is the web service

    <%@ WebService Language="VB" Class="EngineeringDocumentsWebService" %>

     

    Imports System.Web

    Imports System.Web.Services

    Imports System.Web.Services.Protocols

     

    <WebService(Namespace:="http://tempuri.org/")> _

    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _

    <System.Web.Script.Services.ScriptService()> _

    Public Class EngineeringDocumentsWebService

        Inherits System.Web.Services.WebService

       

        <WebMethod()> _

        Public Function GetRevisionStatus(ByVal EngineeringDocumentId As Integer) As String

            Return "RevisionStatus"

        End Function

     

    End Class

     

    I also want to change the background color of the div depending on the resulting value but thought I would get to that after I get it working.

     

    I was also thinging that isn't this the whole purpose of AJAX controls and couldn't I do this somehow with an update panel?

    Filed under:
  • Re: Asynchronously Populating Calculated Columns in a GridView

    03-19-2007, 1:52 PM
    • Member
      459 point Member
    • mtsonic
    • Member since 05-22-2006, 6:37 PM
    • Posts 217

    I realized how this works after some contemplating.  When the page is loaded, meaning all the server side rendering of the html is complete and sent to the browser, the javascript calls the web service which returns a value.  The javascript then makes the innerHTML of the specified tag equal the value which basically replaces the text string in the html of the page.  So,...  to make this work for asp.net controls such as GridViews and DataLists...  (I'm thinking and am no expert)

    1) configure the controls so the data needed for the functions is present in the html.  In my example the Id column would be changed to visible.
    2) in the code-behind (GridView1_RowDataBound in my case) create a control with a unique ID in each row or item.  Something like...
         e.Row.Cells(5).Text = "<DIV style=" & Chr(34) & "WIDTH: 150px; HEIGHT: 20px; BACKGROUND-COLOR: palegreen" & Chr(34) & " id=" & Chr(34) & rowClientId & e.Row.Item(“ID”) & Chr(34) & "></DIV>"
    3) in the javascript pageLoad function, itterate through the <tr> tags looking at the first (in this case) instance of the <td> tag and use it’s innerHTML as a parameter in the function
    4) in the OnComplete function do document.GetElementById(“
    ctl00_ContentPlaceHolder1_GridView1_ctl03_” & parameterValue).innerHTML = results;

    Is this possible and can someone help with the javascript pageLoad function?

    <tr id="ctl00_ContentPlaceHolder1_GridView1_ctl03" style="color:#284775;background-color:White;">

                <td style="white-space:nowrap;">29254</td>

                <td align="center" style="white-space:nowrap;"><DIV style="WIDTH: 150px; HEIGHT: 20px; BACKGROUND-COLOR: palegreen" id="ctl00_ContentPlaceHolder1_GridView1_ctl03_29254"></DIV></td>

    </tr>

     

    The other item I wanted to address was to change the background color of the div tag depending on the value of the results

    Here is what I had in code-behind.
    'With CType(e.Row.FindControl("RevisionStatusLabel"), Label)

    '    Select Case .Text

    '        Case "Not on E.R."

    '            .BackColor = Drawing.Color.LightGray

    '        Case "Preliminary"

    '            .BackColor = Drawing.Color.LightPink

    '            .Font.Bold = True

    '        Case "Current"

    '            .BackColor = Drawing.Color.LightGreen

    '        Case "Being Revised"

    '            .BackColor = Drawing.Color.LightPink

    '            .Font.Bold = True

    '    End Select

    'End With

    Filed under:
  • Re: Asynchronously Populating Calculated Columns in a GridView

    11-11-2009, 12:54 PM
    • Member
      26 point Member
    • mayuresh_s
    • Member since 02-05-2007, 5:42 PM
    • USA
    • Posts 62

    Did you get this resolved, I am having a similar issue of populating a gridview column on row databound due to timeout issue.

    Can you please upload the code or send me an email ?

    Thanks much !

Page 1 of 1 (3 items)