简体   繁体   English

如何在ASP.NET页面中修改HTML和BODY标记而不将runat = server添加到元素中?

[英]How to modify the HTML and BODY tags in an ASP.NET page without adding runat=server to the elements?

In an ASP.NET web forms project that is not quite ready to be upgraded from 2.0 to 4.0 , there is a change that needs to be made to allow modifications to the HTML and BODY tags on the pages without adding runat=server to the tags which results in the 2.0 styled "ugly ids" like "ctrl_100..." 在尚未准备好从2.0升级到4.0的ASP.NET Web表单项目中,需要进行更改以允许修改页面上的HTML和BODY标记而不向标记添加runat = server。结果是2.0样式的“丑陋ID”,例如“ ctrl_100 ...”

For example, how could we change these using JavaScript, a Response Filter (the regex to find these specific tags would really help on this one), or something else...from: 例如,我们如何使用JavaScript,响应过滤器(用于查找这些特定标签的正则表达式确实有助于此操作)或其他方法来更改这些内容,例如:

<html xmlns="//www.w3.org/1999/xhtml">
<body>

To: 至:

<html lang="jp=JP" xmlns="//www.w3.org/1999/xhtml" dir="rtl">
<body dir="rtl">

JavaScript seems like a possibility, but functions from the server-side could would determine the language and direction the page should have. JavaScript似乎是一种可能,但是服务器端的功能可能会确定页面应具有的语言和方向。

If you are able to use jQuery it would make your life easier. 如果您能够使用jQuery,它将使您的生活更轻松。 You could set some hidden fields with the values from your server first and use those to get your values in jquery. 您可以先使用服务器中的值设置一些隐藏字段,然后使用这些字段在jquery中获取您的值。

    <script type="text/javascript">

       $(function(){
          var dir = $("[id$='whatever_you_named_your_hidden_field']").val();
          var lang = $("[id$='whatever_you_named_your_second_hidden_field']").val();

          $('body').attr('dir', dir);
          $('html').attr('lang', lang).attr('dir', dir);
       });

    </script>

Edit Updated my answer based on Chamika Sandamal's answer 编辑根据Chamika Sandamal的答案更新了我的答案

如果您使用的是jQuery,则可以使用以下jQuery代码摆脱那些丑陋的名字,

$("[id$='actualId']")
namespace Website
{
    public static class Utils
    {
        public static string HtmlAttributes()
        {
             return " lang='jp=JP' dir='rtl'";
        }
        public static string BodyAttributes()
        {
             return " dir='rtl'";
        }
    }
}

<html xmlns="//www.w3.org/1999/xhtml"<%= Website.Utils.HtmlAttributes()%>>
<body<%= Website.Utils.BodyAttributes()%>>

If you are sure that there are always some attributes you can remove the first whitespace and add it in markup. 如果确定总是有一些属性,则可以删除第一个空格并将其添加到标记中。

I prefer this method to the javascript method because with the latest the search engines can't know what language or what direction has the html page. 与javascript方法相比,我更喜欢这种方法,因为最新的搜索引擎无法知道html页面的语言或方向。

If you're using .NET 4 you can add ClientIDMode="Static" in the page directive which then forces the ID of the element to remain the same, hence making accessing it from JavaScript much easier. 如果使用的是.NET 4,则可以在page指令中添加ClientIDMode="Static" ,然后强制该元素的ID保持不变,从而使从JavaScript的访问变得更加容易。

You could then use ClientScript.RegisterStartupScript to add some JavaScript from the code behind to manipulate the elements. 然后,您可以使用ClientScript.RegisterStartupScript从后面的代码中添加一些JavaScript来操作元素。

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

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