如何访问.net母版页中的javascript变量

发布于 2024-10-26 13:14:32 字数 188 浏览 1 评论 0原文

我需要启用 .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 技术交流群。

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

发布评论

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

评论(4

有木有妳兜一样 2024-11-02 13:14:58

我可能没有正确理解这个问题,但据我所知,最简单的方法是在母版页上创建一个属性,然后在需要执行脚本的所有页面上设置它:

在母版页中

public bool EnableScript
{
    set
    {
        if (value == true)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "startup", "alert('test');", true);
        }
    }
}

在所有页面中需要运行使用当前母版页的 javascript:

protected void Page_Load(object sender, EventArgs e)
{
    this.Master.EnableScript = true;
}

如果您必须在每个页面上输出 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

public bool EnableScript
{
    set
    {
        if (value == true)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "startup", "alert('test');", true);
        }
    }
}

In all pages that need to run javascript that use the current master page:

protected void Page_Load(object sender, EventArgs e)
{
    this.Master.EnableScript = true;
}

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.

刘备忘录 2024-11-02 13:14:57

您基本上想做的是测试 javascript。

有几种方法可以做到这一点,但我会同时使用服务器端和客户端 cookie。

在你的global.asax的onbeginrequest中,测试“js-test-s”的服务器端cookie

  • Cookie是否存在,检查“js-test-c”
    • “js-test-c”Cookie 存在,JS 已启用
    • "js-test-c" 无 Cookie,禁用 JS
  • 饼干不存在...

    1. 设置“js-test-s”cookie
    2. 输出一个简单的html文档...
      <前>[html]
      [头]
      [元刷新标签设置为1秒/]
      [脚本设置“js-test-c”cookie,然后强制刷新/]
      [/头]
      [正文][/正文]
      [html]
    3. 浏览器将使用设置的一个或两个 Cookie 刷新请求。

警告:您可能希望存储原始请求详细信息,并重定向到另一个位置,以防 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"

  • Cookie Exists, check for "js-test-c"
    • "js-test-c" Cookie Exists, JS enabled
    • "js-test-c" No Cookie, JS disabled
  • Cookie doesn't exist...

    1. Set the "js-test-s" cookie
    2. output a simple html document...

      [html]
        [head]
          [meta refresh tag set to 1 second /]
          [script to set "js-test-c" cookie, then force refresh /]
        [/head]
        [body][/body]
      [html]
    3. Browser will refresh the request with one or both cookies set.

WARNING: You may want to store the original request details, and redirect to another location in case cookies are disabled.

孤独陪着我 2024-11-02 13:14:55

我见过的最灵活的形式是将 JSON 对象作为脚本直接渲染到页面(添加您可能还需要的任何其他脚本),然后使 javascript 代码在表单提交时使用 JSON 数据填充隐藏字段要解析的服务器。

基本步骤如下:

  • 创建一个空的隐藏字段,供服务器在客户端提交时读取,但发送给客户端时为空。
  • 创建一个字符串,它是您需要发送到客户端的脚本和 JSON 数据。 (使用 JavaScriptSerializer .NET 类。)使用
  • script 标记及其内容填充 LiteralControl。 (这在 OnPreRender 步骤中效果很好,但您可以在其他地方执行此操作。)
  • 包含 JSON2.js(它在网络上随处可见 - 从可信来源获取)
  • 使用 JSON2.stringify 编写 javascript 代码来填充提交时的隐藏字段() 将任何 javascript 对象推入该字段。
  • 在服务器上,您可以再次使用 JavaScriptSerializer 从提交的对象中读取值。

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:

  • Create an empty hidden field for the server to read when the client submits, but send it to the client empty.
  • Create a string which is the script and JSON data you need to send to the client. (Use the JavaScriptSerializer .NET class.)
  • Fill a LiteralControl with the script tag and its contents. (This works well in the OnPreRender step, but you can do it elsewhere.)
  • Include JSON2.js (it's all over the web - get it from a trusted source)
  • Write javascript code to fill the hidden field on submit, using JSON2.stringify() to push any javascript object into that field.
  • On the server, you can again use JavaScriptSerializer to read the values from the submitted object.

我能想到的有两种方法。

第一个 if 将变量分配给隐藏的输入字段。可以在 Request.Form 中访问。

html

<input type="hidden" value="your value" name="Hidden1" id="Hidden1"/>

javascript

document.getElementById('Hidden1').value = "an other value";

C#

var myValue = Request.Form["Hidden1"];

第二个是将变量保存到可以在 Request.Cookies 中访问的 cookie 中。

var myValue = Response.Cookies["Cookie1"].Value;

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

<input type="hidden" value="your value" name="Hidden1" id="Hidden1"/>

javascript

document.getElementById('Hidden1').value = "an other value";

C#

var myValue = Request.Form["Hidden1"];

The second is saving the variable into a cookie which can be accessed in Request.Cookies.

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