Here is my code, but it doesn't work (code behind gets empty string): `
<head id="Head1" runat="server">
<title>Pass Javascript Variables to Server</title>
<script type="text/javascript"> // Second Step
function f() {
$("[id$='inpHide']").val("My JavaScript Value");
}
</script>
</head>
<body onload="SetHiddenVariable()">
&l开发者_StackOverflowt;form id="form1" runat="server">
<div>
<input id="inpHide" type="hidden" runat="server" />
<asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f"/>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
`
There were couple of issues in your code. The parenthesis were missing while calling f
function in onClientClick
and if the element has id you can just use the id to select it with #
<head id="Head1" runat="server">
<title>Pass Javascript Variables to Server</title>
<script type="text/javascript"> // Second Step
function f() {
$("input[id*='inpHide']").val("My JavaScript Value");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="inpHide" type="hidden" runat="server" />
<asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f()"/>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
Your <input>
tag needs a name attribute, or it won't be posted.
The response from Sean McMillan is right, but I think you also need to change this :
<asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f"/>
to
<asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f();"/>
精彩评论