[RESOLVED] Passing Array or Object to Javascript Function using ICallbackEventHandler

How would I pass an Object or Array to a function using the ICallBackEventHandler (RaisCallbackEvent)? It allows for a string argument only, if you change it to another datatype it error out. Sample Below...

Public Class _Default
    Inherits System.Web.UI.Page
    Implements ICallbackEventHandler
    Public _callbackResult As String = Nothing


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cbReference As String = Page.ClientScript.GetCallbackEventReference(Me, "arg", "GetDateFromServer", "context")
        Dim cbScript As String = "function UseCallback(arg,context)" & _
        "{" & cbReference & ";" & "}"
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "UseCallback", cbScript, True)
    End Sub


    Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
        Return _callbackResult
    End Function


    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        _callbackResult = Now.ToString
    End Sub


End Class

 

<script language="javascript">
    function GetDate() {
        UseCallback();
    }
    function GetDateFromServer(arg, context) {
        var control = document.getElementById('MainContent_TextBox1');
        control.value = arg
    }
</script>


 

 

 

I dont think you can pass objects...this is from MSDN - 

"Note that the arguments sent between the client script and the server code can only be strings. To pass in or to receive multiple values, you can concatenate values in the input or return string, respectively."

In this case you can use JQuery with Get or POST request....

http://www.intstrings.com/ramivemula/articles/jquery-post-get-request-to-wcf-service-in-asp-net-mvc/

Thanks.

ramiramilu

I dont think you can pass objects...this is from MSDN - 

 

I've been doing some more reseach and came up with the following possible options:

1) doPostBack

2) HiddenField

3) ICallbackEventHandler

4) Page.RegisterClientScriptBlock

I am trying to create samples of each to see which one may be most effective for my situation. Question for someone out there...I basically need an "onfocus" event wired to each dynamic control in a webpage. When the users puts focus on any control, code needs to run on the server to run additional script from a database then return the results to applicable dynamic controls on the web page. How to best do this?

JQuery...

AJAX....Update Panel....

those are best....

Even though you can make this with JAvascript....I would say go for AJAX....because Javascript is clientside code only....but AJAX lays on the Http pipeline itself....os i consider it more efficient....

Thanks. 

ramiramilu

JQuery...

AJAX....Update Panel....

 

I currently am using ajax update panels and can do everything fine with the control server side events like (TextChanged) but I need an onfocus event which is only client side. I need server side code to run when the onfocus is fired.

I dont know whether you are looking at JQuery which I am mentioning from my initial post....you can handle onfocusin, onfocusout, onfoucs events.....

Thanks.

 You may use ClientScript.RegisterArrayDeclaration to work w/ arrays

Here's an example

//aspx
<script language="javascript" type="text/javascript">
        function iterateArray() {
            for (var i = 0; i < newpoints.length; i++) {
                var currentValue = newpoints[i];
                $get("results").innerHTML += '<br>#' + i + ': ' + newpoints[i];
            }
        }
    </script>


//codebehind
//2 examples here

private void ArrayToGoClientSide()
    {
        string clientSideArrayName = "newpoints";
        string arrayString = string.Empty;
        for (int i = 0; i < 6; i++)
        {
            if (arrayString.Length > 0)
                arrayString += ",";

            arrayString += "'" + "test"+ i.ToString() + "'";
        }
        ClientScript.RegisterArrayDeclaration(clientSideArrayName, arrayString);
    }


    private void ArrayToGoClientSide2()
    {
        String arrName = "MyArray";
        String arrValue = "\"aaaa\", \"sssss\", \"icon0\", \"title\", \"description\"";
        String csname1 = "PopupScript";
        String csname2 = "ButtonClickScript";

        if (!Page.ClientScript.IsClientScriptBlockRegistered(csname1))
        {
            String cstext1 = "<script type=\"text/javascript\">" +
                "alert('Hello World');</" + "script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(),csname1, cstext1);
        }

        if (!Page.ClientScript.IsClientScriptBlockRegistered(csname2))
        {
            StringBuilder cstext2 = new StringBuilder();
            cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
            cstext2.Append("Form1.Message.value='Text from client script.'} </");
            cstext2.Append("script>");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),csname2, cstext2.ToString());
        }

    }


 

thoughts on "[RESOLVED] Passing Array or Object to Javascript Function using ICallbackEventHandler"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>