C# 在Javascript中设置隐藏的输入值,然后在codebehind中访问它
我一直在尝试使用Javascript设置隐藏输入的值,然后从我的C#codebehind中访问该值。当我运行下面复制的代码时,分配给AssignedDS的值是“”,我假设它是隐藏输入的默认值。如果我手动设置html标记中的值,则assignedIDs将设置为该值 此行为向我表明,输入值正在onClientClick和onClick事件之间重置(重新呈现?) 我将非常感谢您对这件事的任何帮助。我花了几个小时试图解决一个看起来很简单的问题 html/javascript:C# 在Javascript中设置隐藏的输入值,然后在codebehind中访问它,c#,asp.net,javascript,html,C#,Asp.net,Javascript,Html,我一直在尝试使用Javascript设置隐藏输入的值,然后从我的C#codebehind中访问该值。当我运行下面复制的代码时,分配给AssignedDS的值是“”,我假设它是隐藏输入的默认值。如果我手动设置html标记中的值,则assignedIDs将设置为该值 此行为向我表明,输入值正在onClientClick和onClick事件之间重置(重新呈现?) 我将非常感谢您对这件事的任何帮助。我花了几个小时试图解决一个看起来很简单的问题 html/javascript: <html xmln
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Admin Page - Manage Tasks</title>
<script language="javascript" type="text/javascript">
function PopulateAssignedIDHiddenInput() {
var source = document.getElementById('assignedLinguistListBox');
var s = "";
var count = source.length;
for (var i = count - 1; i >= 0; i--) {
var item = source.options[i];
if (s == "") { s = source.options[i].value; }
else { s = s.concat(",",source.options[i].value); }
}
document.getElementById('assignedIDHiddenInput').Value = s;
// I have confirmed that, at this point, the value of
// the hidden input is set properly
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel id="EditMode" runat="server">
<table style="border: none;">
<tr>
<td>
<asp:Label ID="availableLinguistLabel" runat="server" Text="Available"></asp:Label><br />
<asp:ListBox ID="availableLinguistListBox" runat="server" Rows="10" SelectionMode="Multiple"></asp:ListBox>
</td>
<td>
<input type="button" name="right" value=">>"
onclick="Javascript:MoveItem('availableLinguistListBox', 'assignedLinguistListBox');" /><br /><br />
<input type="button" name="left" value="<<"
onclick="Javascript:MoveItem('assignedLinguistListBox', 'availableLinguistListBox');" />
</td>
<td>
<asp:Label ID="assignedLinguistLabel" runat="server" Text="Assigned To"></asp:Label><br />
<asp:ListBox ID="assignedLinguistListBox" runat="server" Rows="10" SelectionMode="Multiple"></asp:ListBox>
</td>
</tr>
</table>
//-snip-
<asp:Button ID="save_task_changes_button" runat="server" ToolTip="Click to save changes to task"
Text="Save Changes" OnClick="save_task_changes_button_click" OnClientClick="Javascript:PopulateAssignedIDHiddenInput()" />
</asp:Panel>
<!-- Hidden Inputs -->
<!-- Note that I have also tried setting runat="server" with no change -->
<input id="assignedIDHiddenInput" name="assignedIDHiddenInput" type="hidden" />
</div>
</form>
</body>
在javascript中,需要将
值属性设置为小写,如下所示:
document.getElementById('assignedIDHiddenInput').value = s;
var source = document.getElementById('assignedLinguistListBox');
var opts = [];
for (var i = 0; i < source.options.length; i++) {
opts.push(source.options[i].value);
}
var s = opts.join(',');
var s = $('#assignedLinguistListBox option').map(function() {
return this.value;
}).get().join(',');
$('#assignedIDHiddenInput').val(s);
然后将正确设置:)
虽然如果您提醒.Value
它将显示您的值,但实际上您已经添加了一个新的.Value
属性,但您尚未设置输入的.Value
属性,该属性就是发布到服务器的属性。上面的示例链接从两个方面说明了这一点
此外,您还可以通过使用数组而不是字符串串联来加快速度,特别是如果您有很多选项,如:
document.getElementById('assignedIDHiddenInput').value = s;
var source = document.getElementById('assignedLinguistListBox');
var opts = [];
for (var i = 0; i < source.options.length; i++) {
opts.push(source.options[i].value);
}
var s = opts.join(',');
var s = $('#assignedLinguistListBox option').map(function() {
return this.value;
}).get().join(',');
$('#assignedIDHiddenInput').val(s);
我假设这里是ASP.NET
如果是这样,您的问题是ASP.NET生成的HTML中控件的id不会是您在脚本中引用的“AssignedHiddenInput”。ASP.NET会在以声明方式呈现HTML页面中指定的HTML之前更改这些内容。在页面上查看源代码,你就会明白我的意思
以下是一种解决方法:
document.getElementById('<%=assignedIDHiddenInput.ClientID %>').value = s;
document.getElementById(“”).value=s;
更新:如注释中所述,这仅在控件设置为RunAt=Server时才相关。我认为ASP.NET调用javascript在该控件上执行回发,然后再调用javascript函数填充该隐藏值
我认为可以禁用默认回发并自己处理,但我相信其他人可以给出更好的建议
在您的函数中插入一个alert(),以查看在触发回发之前是否真的调用了它。这不是问题,他的输入不是runat=“server”
。我一直认为只有在“runat=server”时才为您分配了一个clientid?这一点很好。他提到了“”,所以我认为他可能希望以这种方式运行它,但他的服务器端代码使用了“Request.Form[“assignedIDHiddenInput”]约翰,谢谢你的输入。我忘了提到我也曾尝试过用这种方式对输入进行寻址。非常感谢!哦…哇!我不敢相信!这是一个愚蠢而简单的错误。谢谢你,尼克!用for…in
语句迭代选项对象会给你带来意想不到的结果d结果,在某些浏览器中,即使数值选项索引也不会被迭代,我建议对
循环使用一个普通的,检查@CMS-浏览器依赖性上的要点很好,这里也习惯了每个
闭包。我更新了上面的代码,以防其他人以后遇到需要相同信息的情况。是的,我认为for…in
语句,因为它们的真正目的是迭代数值索引,而不是枚举对象属性(即使知道这些索引是属性)此外,对于主机对象,您永远无法确定将找到哪些属性……顺便说一句,这是一篇关于此主题的好文章: