简体   繁体   English

动态将JS添加到ASP.NET页并从JS获取结果

[英]Dynamically Add JS to an ASP.NET page and get results from the JS

Here's my issue: 这是我的问题:

Client(s) give me separate JS files which will run a check of some sort on the user's system (Browser type, are cookies enabled?, etc.) and a list of acceptable values to be returned from this check. 客户端为我提供了单独的JS文件,这些文件将在用户的系统上运行某种检查(浏览器类型,是否启用cookie?等),以及将从此检查返回的可接受值的列表。

I want to run through each JS file when a user visits the site and compare the results to the list of acceptable values, then alert the user if they pass these requirements or not. 我想在用户访问站点时浏览每个JS文件并将结果与​​可接受值列表进行比较,然后在用户通过或未通过这些要求时提醒用户。

I'm currently using RegisterClientScriptBlock() to add the JS to the client's page, so it's being run, but I'm having issues getting the result value from the JS back to ASP.NET in order to do the comparison. 我目前正在使用RegisterClientScriptBlock()将JS添加到客户端页面,因此正在运行它,但是在进行比较时,我遇到了将JS的结果值返回到ASP.NET的问题。

I've tried using hidden fields that the JS will dump the value to and ASP.NET will read from, but I'm having difficulty generating the hidden fields on demand (since I have no idea how many Js files the client could have) and have them work in the ASP.NET code. 我尝试使用JS将值转储到的隐藏字段并读取ASP.NET,但是我很难按需生成隐藏字段(因为我不知道客户端可以拥有多少个Js文件)并让它们以ASP.NET代码运行。

Any help, or suggestions in the right direction would be awesome, thanks! 任何帮助或朝着正确方向的建议都将非常有用,谢谢!

What I would do is have the results be an array of KeyValuePair objects that you would then serialize to JSON. 我要做的是使结果为KeyValuePair对象数组,然后将其序列化为JSON。 So you create the javascript object type like so: 因此,您可以像这样创建javascript对象类型:

function KeyValuePair(key, value){
     this.Key = key;
     this.Value = value;
}

Then you would build up an array of KeyValuePairs like so: 然后,您将像这样建立一个KeyValuePairs数组:

//This array is declared in the global scope 
var ValueArray = new Array();

function someFunction(){
   //this assumes that the key and value variables are created earlier in the function
   var valueToStore = new KeyValuePair(key, value);
   ValueArray[ValueArray.length] = valueToStore;
}

So at the point when you are done with all your checks you would use the json2 serializer to serialize the array to json for storage in your hidden field. 因此,在完成所有检查后,您将使用json2序列化程序将数组序列化为json,以存储在隐藏字段中。

var jsonToSaveToHiddenField = JSON.stringify(ValueArray);
//Logic to store resulting json and trigger the serverside evaluation here

On the server side you would use JavascriptSerializer to deserialize your json to an array of KeyValuePairs. 在服务器端,您可以使用JavascriptSerializer将json反序列化为KeyValuePairs数组。 Here is the msdn doc on that: JavaScriptSerializer Class Reference 这是关于此的msdn文档: JavaScriptSerializer类参考

So with this approach you only need one hidden field. 因此,使用这种方法,您只需要一个隐藏字段。 So you don't need to dynamically create it which should simplify the server side retrieval quite a bit. 因此,您无需动态创建它就可以大大简化服务器端的检索。

The above should work with minimal changes however I haven't run this through a compiler so there might be some minor syntax errors preset. 上面的代码应该进行最小的更改,但是我还没有通过编译器运行它,因此可能会预置一些小的语法错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM