如何访问.net母版页中的javascript变量
我需要启用 .net 母版页中存在的脚本,仅在使用该母版页的选定 html 页面中启用。这可以通过在我需要此脚本的html页面的javascript中声明一个变量并将该变量设置为某个值来实现,以便仅当变量为NotNullorEmpty时我才能启用母版页中的脚本吗?有人知道这是否有效吗?如果是这样,如何在.net母版页中获取html中的javascript变量?
I need to enable a script which is present in a .net master page, in only selected html pages that use this master page. can this be achieved by declaring a variable in javascript of html pages where I need this script and set the variable to some value, so that I can enable the scripts in master page only when variable is NotNullorEmpty? Does some one know if this works. If so how to get javascript variable in html, in to .net master page ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可能没有正确理解这个问题,但据我所知,最简单的方法是在母版页上创建一个属性,然后在需要执行脚本的所有页面上设置它:
在母版页中
在所有页面中需要运行使用当前母版页的 javascript:
如果您必须在每个页面上输出 JavaScript,即使是在不执行它的那一次(为什么?),那么只需将其添加为函数,然后在属性中添加调用该函数的代码,并在需要 JavaScript 执行的所有页面上将属性设置为 true。
I might be not understanding the question correctly but as far as I can tell the easiest way to do this is by creating a property on the master page and then setting it on all pages that need to execute the script:
In Master page
In all pages that need to run javascript that use the current master page:
If you have to output your JavaScript on every page, even on the once that don't execute it (why?) then just add it as a function and then inside your property add a code to call that function, and set your property to true on all pages that require JavaScript to execute.
您基本上想做的是测试 javascript。
有几种方法可以做到这一点,但我会同时使用服务器端和客户端 cookie。
在你的global.asax的onbeginrequest中,测试“js-test-s”的服务器端cookie
<前>[html]
[头]
[元刷新标签设置为1秒/]
[脚本设置“js-test-c”cookie,然后强制刷新/]
[/头]
[正文][/正文]
[html]
警告:您可能希望存储原始请求详细信息,并重定向到另一个位置,以防 cookie 被禁用。
What you are basically wanting to do is test for javascript.
There are a few ways to do this, but I would use both a server-side and client-side cookie.
In your global.asax's onbeginrequest, test for a server-side cookie of "js-test-s"
WARNING: You may want to store the original request details, and redirect to another location in case cookies are disabled.
我见过的最灵活的形式是将 JSON 对象作为脚本直接渲染到页面(添加您可能还需要的任何其他脚本),然后使 javascript 代码在表单提交时使用 JSON 数据填充隐藏字段要解析的服务器。
基本步骤如下:
JavaScriptSerializer
.NET 类。)使用script
标记及其内容填充LiteralControl
。 (这在 OnPreRender 步骤中效果很好,但您可以在其他地方执行此操作。)The most flexible form of this that I have seen is to render a JSON object directly to the page as script (adding any other script you might need also), then making the javascript code fill in a hidden field with JSON data on form submit for the server to parse.
Here are the basic steps:
JavaScriptSerializer
.NET class.)LiteralControl
with thescript
tag and its contents. (This works well in the OnPreRender step, but you can do it elsewhere.)JavaScriptSerializer
to read the values from the submitted object.我能想到的有两种方法。
第一个 if 将变量分配给隐藏的输入字段。可以在 Request.Form 中访问。
html
javascript
C#
第二个是将变量保存到可以在 Request.Cookies 中访问的 cookie 中。
There are 2 ways I can think of.
The first if to assign the variable to a hidden input field. Which can be accessed in the Request.Form.
html
javascript
C#
The second is saving the variable into a cookie which can be accessed in Request.Cookies.