简体   繁体   English

尝试使用从Javascript到C#的隐藏字段发送值

[英]Trying to send value using Hidden Field from Javascript to C#

I have a C#, ASP.NET Website, i have a JavaScript script inside it and i wish to send a var from inside of it to my code behind function, i tried to use a hidden field to do it, here's how it looks: 我有一个C#,ASP.NET网站,里面有一个JavaScript脚本,我希望从它内部发送一个var到我的代码后面的函数,我试着用一个隐藏的字段来做它,这是它的样子:

<asp:HiddenField ID="chatMessage" runat="server" />
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.min.js" "></script>
<!--Reference the SignalR library. -->
<script src="/Scripts/jquery.signalR-2.0.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<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();

            var chatMessage = document.getElementById('chatMessage');
            chatMessage.value = 'value from javascript';

            // Add the message to the page.
            $('#discussion').append('<li class="<%=returnLiClass(chatMessage.Value)%><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 send chatMessage.Value to my returnLiClass() function. 如您所见,我将chatMessage.Value发送到我的returnLiClass()函数。 When i debug i see it gets an empty string (""). 当我调试时,我看到它得到一个空字符串(“”)。

What am i doing wrong? 我究竟做错了什么? Thank you! 谢谢!

When working with any server controls if you wish to reference them in javascript you have to use the ClientID. 使用任何服务器控件时,如果您希望在javascript中引用它们,则必须使用ClientID。

It should look like the following: 它应该如下所示:

var chatMessage = document.getElementById('<%= chatMessage.ClientID %>');

试试jquery

$('#chatMessage').val(chatMessage.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM