Sie*_*ith 6 html javascript c# asp.net
我一直在尝试使用Javascript设置隐藏输入的值,然后从我的C#代码隐藏中访问该值.当我运行下面复制的代码时,分配给assignedID的值是"",我假设它是隐藏输入的默认值.如果我在html标记中手动设置值,则assignIDs设置为该值.
这个行为告诉我,在onClientClick和onClick事件触发之间输入的值被重置(重新渲染?).
我很感激任何有关此事的帮助.我花了好几个小时试图解决看似非常简单的问题.
HTML/JavaScript的:
<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>
Run Code Online (Sandbox Code Playgroud)
C#
protected void save_task_changes_button_click(object sender, EventArgs e)
{
string assignedIDs = Request.Form["assignedIDHiddenInput"];
// Here, assignedIDs == ""; also, Request.Params["assignedIDHiddenInput"] == ""
// -snip-
}
Run Code Online (Sandbox Code Playgroud)
在javascript中,您需要将该value属性设置为小写,如下所示:
document.getElementById('assignedIDHiddenInput').value = s;
Run Code Online (Sandbox Code Playgroud)
然后它将被正确设置:) 你可以在这里看到一个实例
虽然如果你警告.Value它会显示你的价值,你实际上已经添加了一个新 .Value属性,但你还没有设置输入的.value属性,这是发布到服务器的属性.上面的示例链接说明了这两种方式.
此外,你可以使它更快一点,特别是如果你有很多选项使用数组而不是字符串连接,如下所示:
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(',');
Run Code Online (Sandbox Code Playgroud)
编辑:上面的代码更新,CMS是正确的,以前的方法是依赖浏览器的,上面的行为现在应该是一致的.此外,如果jQuery是一个选项,有更短的方式来获取此信息,如下所示:
var s = $('#assignedLinguistListBox option').map(function() {
return this.value;
}).get().join(',');
$('#assignedIDHiddenInput').val(s);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31204 次 |
| 最近记录: |