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

发布于 2024-10-11 18:16:50 字数 389 浏览 4 评论 0原文

这是我的问题:

客户端给我单独的 JS 文件,这些文件将在用户系统上运行某种检查(浏览器类型、是否启用 cookie?等)以及从此检查返回的可接受值的列表。

我想在用户访问网站时运行每个 JS 文件,并将结果与​​可接受值列表进行比较,然后提醒用户是否通过了这些要求。

我目前正在使用 RegisterClientScriptBlock() 将 JS 添加到客户端页面,因此它正在运行,但我在将结果值从 JS 获取回 ASP.NET 以便进行比较时遇到问题。

我尝试过使用 JS 将值转储到其中并由 ASP.NET 读取的隐藏字段,但我很难按需生成隐藏字段(因为我不知道客户端可以有多少个 Js 文件)并让它们在 ASP.NET 代码中工作。

任何帮助或正确方向的建议都会很棒,谢谢!

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.

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.

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.

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.

Any help, or suggestions in the right direction would be awesome, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等数载,海棠开 2024-10-18 18:16:50

我要做的就是让结果成为 KeyValuePair 对象的数组,然后将其序列化为 JSON。因此,您可以像这样创建 javascript 对象类型:

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

然后您将像这样构建一个 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;
}

因此,当您完成所有检查后,您将使用 json2 序列化器 将数组序列化为 json 以存储在隐藏字段中。

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

在服务器端,您可以使用 JavascriptSerializer 将 json 反序列化为 KeyValuePairs 数组。这是关于该问题的 msdn 文档: JavaScriptSerializer 类参考

因此,通过这种方法,您只需要一个隐藏字段。因此,您不需要动态创建它,这应该会大大简化服务器端检索。

上面的代码应该可以进行最小的更改,但是我还没有通过编译器运行它,因此可能会预设一些小的语法错误。

What I would do is have the results be an array of KeyValuePair objects that you would then serialize to JSON. So you create the javascript object type like so:

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

Then you would build up an array of KeyValuePairs like so:

//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.

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. Here is the msdn doc on that: JavaScriptSerializer Class Reference

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文