[英]Access jquery variable from the code behind
这是我的html:
<input type="hidden" id="HiddenIndex" name="HiddenIndex" runat="server" />
一些标签和文本框
<asp:Button runat="server" ID="btnGetCoordinates"
Text="Get Coordinates" OnClick="btnGetCoordinates_Click" />
<asp:Label ID="info" runat="server" Text="Waiting...." />
因此,当单击获取坐标按钮时,它将调用Web服务从后面的代码返回一些json结果。 对话框将弹出带有这些结果的列表框。 到现在为止,它都可以正常工作。 我的目标是当客户在列表中选择一个项目并单击“选择”按钮时,它将返回所选项目的索引,存储在隐藏字段中,并稍后从后面的代码中进行操作。 这是我的jQuery函数
function ShowPopup()
{
$("#parentForm").fadeTo(500, .2);
$("#C1Dialog1").dialog({
open: function () {
$(".ui-dialog-titlebar-close").hide();
},
buttons: [{
text: "Select",
click: function () {
var value = " ";
storedIndex = " ";
var selected = $("[id*=lstCandidates] option:selected");
selected.each(function () {
value = $(this).val();
storedIndex = $(this).index();
$("#HiddenIndex").val(storedIndex);
});
alert(value + " and index is " + storedIndex); //Show value and index
alert("html hidden value " + $("#HiddenIndex").val()); //show value
$(this).dialog("close");
$("#parentForm").fadeTo(500, 1);
},
style: "margin-right: 40px;"
},
{
text: "Cancel",
click: function () {
$(this).dialog("close");
$("#parentForm").fadeTo(500, 1);
},
style: "margin-left:0px;"
}]
});
}
</script>
如您所见,警报显示隐藏字段的值。这是我的代码
protected void btnGetCoordinates_Click(object sender, EventArgs e)
{
//Show the Dialog
if (count > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup();", true);
// /请不要在这里停止--问题在这里**,“隐藏字段”没有任何值**
var indexValue = Request.Form["HiddenIndex"];
info.Text = "HiddenIndex is " + indexValue;
}
}
单击“对话框”的选择按钮时,“信息”标签不显示任何内容。非常感谢您的帮助。
客户端ID可能有问题。 发出HTML时,ASP.NET不一定会在标记中使用客户端ID。
有几种方法可以解决此问题,但最简单的方法是使隐藏字段成为纯HTML控件。 这样,ASP.NET就不会再胡闹了。 所以改变这个
<input type="hidden" id="HiddenIndex" name="HiddenIndex" runat="server" />
对此
<input type="hidden" id="HiddenIndex" name="HiddenIndex"/>
其他选项:
id$='HiddenIndex'
,使其忽略由ASP.NET添加的任何前缀 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.