我正在使用以下形式的ASP.NEt hiddenfield:
<form id="form1" runat="server">
<asp:HiddenField ID="ScreenWidth" runat="server" />
<asp:HiddenField ID="ScreenHeight" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>
这是JS代码:
<script type="text/javascript">
$(document).ready(function() {
alert(document.getElementById("ScreenWidth").val);
document.getElementById("ScreenWidth").val = screen.width;
document.getElementById("ScreenHeight").val = screen.height;
alert(document.getElementById("ScreenWidth").val);
});
但是当我访问onsubmit函数上的值时-它们为空。 警报显示未定义,然后在触发时显示值-JS。部分有效...
如何获取c#代码中的值?
myheight = ScreenHeight.Value.ToString();
mywidth = ScreenWidth.Value.ToString();
答案 0 :(得分:2)
使用toString();在js中:
document.getElementById("hidScreenWidth").value = screen.width.toString();
隐藏代码:
string w = hidScreenWidth.Value;