我的ASP.NET网站中有一个JavaScript脚本,我想从函数中获取一个值到我的C#后端,使用我从ASP.NET隐藏字段传递的参数。
这是我的ASP.NET& JavaScript代码,我定义了一个HiddenField并为其分配了chatMessage
var,然后为var赋值,并尝试将其值发送给returnLiClass()
函数。
(最相关的是第1,第13,第14,第17行):
<asp:HiddenField ID="chatMessage" runat="server" />
<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.ClientID %>');
chatMessage.value = 'value from javascript';
// Add the message to the page.
$('#discussion').append('<li class="<%=returnLiClass(chatMessage.Value)%><strong>' + encodedName
+ '</strong>: ' + 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>
后端函数(当我调试msg
和chatMessage.Value
时都没有值):
protected String returnLiClass(String msg)
{
String test = chatMessage.Value;
return "redChat";
}