C# asp.net将值从JS/jquery传递到c代码后面#
我已经尝试了“每一种”可能的方法,将screen.width vlaue从aspx页面上的JS脚本发送到后面代码中的c#,虽然我可以看到screen.width被正确分配,但它从未被分配到我的隐藏字段值C# asp.net将值从JS/jquery传递到c代码后面#,c#,javascript,jquery,asp.net,code-behind,C#,Javascript,Jquery,Asp.net,Code Behind,我已经尝试了“每一种”可能的方法,将screen.width vlaue从aspx页面上的JS脚本发送到后面代码中的c#,虽然我可以看到screen.width被正确分配,但它从未被分配到我的隐藏字段值 <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <asp:HiddenField ID="hiddenfield" runat="server" />
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:HiddenField ID="hiddenfield" runat="server" />
<script type="text/javascript" language="javascript">
$(function(){
$('#hiddenfield').val(screen.width);
});
</script>
other content
</asp:Content>
无论我尝试什么,s总是“
上面有点问题,每个人似乎都是这样做的,而且它工作正常?渲染的隐藏字段的ID不是“hiddenfield”-它将类似于ctl00\u bodycontent\u hiddenfield 试用
$('[id$="hiddenfield"]')
作为选择器。这应该得到正确的选择器:
$('#<%= hiddenfield.ClientID %>').val(screen.width);
$('#').val(屏幕宽度);
检查页面的视图源并找出元素的正确id,然后在其上使用jquery选择器,然后在页面加载检查request.form集合以检查隐藏变量是否在post请求中
<asp:HiddenField ID="hiddenfield" runat="server" ClientIDMode="Static">
</asp:HiddenField >
如果使用ASP.NET 4或
$('#<%= hiddenfield.ClientID %>').val(screen.width);
$('#').val(屏幕宽度);
ID
是ASP.net控件ID,因此lblMyLabel
例如,ClientID
是发送到浏览器的ID,它与根级别的ID
相同,但对于母版页和WebControl,它将以其ID作为前缀,例如namespace\u MasterPage\u webControlID\u controlID
。
$('#<%= hiddenfield.ClientID %>').val(screen.width);