0

I have my ASP.NET C# Web site and i have some JavaScript script inside one of my pages, i already succeeded in calling a C# function from my Code Behind but i want to send some variable from my JavaScript script as an argument of the function.

Here is my code, including the C# function call:

<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub.
        var chat = $.connection.chatHub;
        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMessage = function (name, message) {
            // Html encode display name and message.
            var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div /> ').text(message).html();
            var tremp_id = $('<div /> ').text("<%=Request.QueryString["trempid"]%>").html();

            // Add the message to the page.
            $('#discussion').append('<li class="<%=returnLiClass()%><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + "Tremp:" + tremp_id + '</li>');
        };
        // Get the user name and store it to prepend to messages.
        $('#displayname').val('<%=returnName()%>');
        // Set initial focus to message input box.
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
    });
</script>

As you can see I'm calling the 'returnLiClass()' function (line 13) and i want to send the 'encodedMsg' var inside it.

How can i do it? Thanks!

1
  • One technique you can use, is to put your value in hidden field with the runat="server" attribute. Commented May 26, 2015 at 14:12

1 Answer 1

0

Place a function with name same as client method in your Hub class.

void broadcastMessage(string name, string message)
{
   //Do something here.....
}

Please note that the parameter name must be same.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.