Link to home
Start Free TrialLog in
Avatar of amandajdarlow
amandajdarlow

asked on

How to call a function on the onclick of a DevExpress pivot grid cell click

I currently have the following which works perfectly.  This sets the onclick event of a DevExpress pivot grid cell click to open a new window.

Public Sub New(text As String, strEpisodes As String, values As List(Of Object))

        Dim tagURL As String

        If text <> String.Empty Then
            Me.NavigateUrl = "#"
            tagURL = "window.open('SessionTagNotes.aspx?tag=" & text & "&episodes=" & strEpisodes & "', 'winTagNotes', 'directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no,top=5,left=0,height=500,width=800', true);"
            Attributes("onclick") = tagURL
        End If

End Sub

Open in new window


I would like to change it so that it calls a function rather than opening a new window.  So I tried the following

Public Sub New(text As String, strEpisodes As String, values As List(Of Object))

       Dim tagURL As String

        If text <> String.Empty Then
            tagURL = TWSLibrary.TagGridNotes(text, strEpisodes)
            Attributes("onclick") = tagURL
        End If


End Sub

Open in new window


However this causes the function to run at the time this sub is executed rather than when the user clicks on the cell.  I need the function to execute when the user clicks on the cell in the pivot grid.

This code is all embedded in the below which I have put in for completeness.

Can you please tell me where I'm going wrong.

Thanks.

Imports DevExpress.Web.ASPxPivotGrid
Imports DevExpress.XtraPivotGrid.Data

Public Class FieldValueTemplate
    Implements ITemplate

    Public Sub InstantiateIn(control As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn

        Dim c As PivotGridFieldValueTemplateContainer = DirectCast(control, PivotGridFieldValueTemplateContainer)
        Dim cell As PivotGridFieldValueHtmlCell = c.CreateFieldValue()
        Dim valueItem As PivotFieldValueItem = c.ValueItem
        Dim helperArgs As PivotFieldValueEventArgs = New PivotFieldValueEventArgs(valueItem)
        Dim fields() As PivotGridField = helperArgs.GetHigherLevelFields()

        For Each field As PivotGridField In fields

            Dim currentValue As Object = helperArgs.GetHigherLevelFieldValue(field)

            If cell.TextControl.Text = "" Then
                cell.Controls.AddAt(cell.Controls.IndexOf(cell.TextControl), New LiteralControl(c.Text))
                cell.Controls.Remove(cell.TextControl)
            End If

            c.Controls.Add(cell)

        Next

    End Sub

End Class

Public Class CellTemplate
    Implements ITemplate
    Dim strEpisodes As String

    Sub New(Episodes As String)
        strEpisodes = Episodes
    End Sub

    Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
        Dim c As PivotGridCellTemplateContainer = DirectCast(container, PivotGridCellTemplateContainer)
        Dim fieldValues As List(Of Object) = New List(Of Object)

        If Not IsNothing(c.ColumnField) Then
            fieldValues.Add(c.GetFieldValue(c.ColumnField))
        End If

        If Not IsNothing(c.RowField) Then
            fieldValues.Add(c.GetFieldValue(c.RowField))
        End If

        If c.Text.Contains(",") Then
            Dim strTagArr() As String = c.Text.Split(",")
            For Each strTag As String In strTagArr
                c.Controls.Add(New MyLink(strTag, strEpisodes, fieldValues))
            Next
        Else
            c.Controls.Add(New MyLink(c.Text, strEpisodes, fieldValues))
        End If

    End Sub
End Class

Public Class MyLink
    Inherits HyperLink

    Public Sub New(text As String, strEpisodes As String, values As List(Of Object))

        Dim tagURL As String

        If text <> String.Empty Then
            tagURL = TWSLibrary.TagGridNotes(text, strEpisodes)
            Attributes("onclick") = tagURL
            text = String.Empty
        End If


    End Sub
End Class

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

If this is a JavaScript function then change

tagURL = TWSLibrary.TagGridNotes(text, strEpisodes)

to

tagURL = "TWSLibrary.TagGridNotes(text, strEpisodes);"


If its a VB.NET function on the server side then it will need to a bit more work to relay the client side click to the server.
Avatar of amandajdarlow
amandajdarlow

ASKER

It is a vb.net function which simply sets two session variables.  Can this be done in javascript?
Session is a server side structure so you cannot directly set it in JavaScript. What you can do is call the server side method in JavaScript.

Examples

http://www.morgantechspace.com/2014/01/Call-Server-Side-function-from-JavaScript-in-ASP-NET.html

http://www.c-sharpcorner.com/UploadFile/rohatash/calling-server-side-function-from-javascript-in-Asp-Net/
I decide to write a Javascript function which assigned the required value to a hidden field instead of trying to set a Session Variable.

However I cannot get the Javascript function to execute.

The JS function is this.
 function UpdateTagNotes(intTagId) {
            document.getElementById('hdnTagGridTagId').value = intTagId;
            alert("Hi");
        }

The calling code is this

tagURL = "UpdateTagNotes(" & intTagId & ");"
Attributes("onclick") = tagURL

or this

tagURL = "Page.ClientScript.RegisterClientScriptBlock(Me.GetType, 'UpdateTagNotes', '<script language='javascript'>UpdateTagNotes(" & intTagId & ");</script>')"
Attributes("onclick") = tagURL

neither works.

Any thoughts?  I am tearing my hair out.

Thanks
Does changing

tagURL = "UpdateTagNotes(" & intTagId & ");"

to

tagURL = "Javascript:UpdateTagNotes(" & intTagId & ");"

help?
Nope still not working.

However if I add

  function UpdateTagNotes() {
            hdnTagGridTagId.Set("TagId", "999");
        }
        function GetValue() {
            alert(hdnTagGridTagId.Get("TagId"));
        }

and

    <a href="javascript:GetValue()">Get value</a>
    <a href="javascript:UpdateTagNotes()">Set value</a>

then these button call the functions no problem.

But the line

ClientScript.RegisterClientScriptBlock(Me.GetType, "debtnLstTagGrid_SelectedIndexChanged", "<script language='javascript'>UpdateTagNotes(1);</script>")

won't work either!?!?!?!

Many thanks for your help.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
At last it is all working.

Turned out to be a combination of DevExpress settings which was causing the problem.  Javascript was fine all along.

Many thanks for all the help.
All your help was much appreciated.
Glad you got it working :-)