[英]Pass Javascript value into C# Code Behind (Scanning)
I've got a scanner that calls a JavaScript function when it scans a barcode. 我有一个扫描器,在扫描条形码时会调用JavaScript函数。 In this function, I am trying to pass the barcode value into some kind of variable that I can use in my C# code behind.
在此功能中,我试图将条形码值传递给某种变量,该变量可以在后面的C#代码中使用。 I've tried this:
我已经试过了:
document.getElementById("hidden").value = data;
alert(document.getElementById("hidden").value);
This gets the barcode value scanned and alerts it. 这将扫描条形码值并发出警报。 In the code behind, I am looking for something like this:
在后面的代码中,我正在寻找这样的东西:
protected void hidden_ValueChanged(object sender, EventArgs e)
{
//magic wizard man stuff going on
}
This doesn't seem to trigger at all. 这似乎根本不会触发。 Any ideas?
有任何想法吗?
(Essentially I want a value to get passed into C# Code Behind so I can do stuff with it) (基本上,我希望将值传递到C#代码隐藏中,以便我可以对其进行处理)
您最终必须做一个回发,但是如果可以创建一个按钮并从javascript代码中单击它,那可能是一个更简单的解决方案。
Attach that event to hidden field like this 像这样将事件附加到隐藏字段
<asp:hiddenfield id="hidden"
onvaluechanged="hidden_ValueChanged"
value=""
runat="server"/>
In order to achieve this, you can use, Page Method. 为了实现这一点,可以使用Page Method。 The other alternate is to use jquery, like this(just a code snippet):
另一个替代方法是使用jquery,如下所示(只是一个代码片段):
$.ajax({
type: "POST",
url: "WebForm.aspx/CodeBehindMethod",
data: "{<your scanner id>}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg)
{
//any div or label you want to update
}
})
And in your code behind: 并在后面的代码中:
[WebMethod]
public static string CodeBehindMethod(string Id)
{
//whatever you want to do with the Id
return Id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.