javascript 根据 Web.config Key 呈现 ASP.NET 条件标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4734787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:43:28  来源:igfitidea点击:

ASP.NET Conditional Markup Rendering According to Web.config Key

c#javascriptasp.netmarkup

提问by Parag Meshram

I have a key in web.config as -

我在 web.config 中有一个键 -

<add key="IsDemo" value ="true"/>

I want to show/hide markup based on above web.config entry for a non-server html tagwithout using code behind file (as there is no .cs file and there are no runat=server controls). Something similar to following pseudo code:

我想显示/隐藏基于上述 web.config 条目的非服务器 html 标记的标记,而不使用代码隐藏文件(因为没有 .cs 文件并且没有 runat=server 控件)。类似于以下伪代码的东西:

IF ( IsDemo == "true" )
THEN
<tr>
    <td id="tdDemoSection" colspan="2" align="left" valign="top">
        <.....>
    </td>
</tr>
ENDIF

Does anyone know that we can write such conditional logic in .aspx markup? Please help!!!

有谁知道我们可以在 .aspx 标记中编写这样的条件逻辑?请帮忙!!!

EDIT:

编辑:

Section I'm hiding or showing have some data like username and password. So, I do not want user to use Firebug or Web Developer Tools to see hidden markup. markup should not go to client side.

我隐藏或显示的部分有一些数据,如用户名和密码。因此,我不希望用户使用 Firebug 或 Web Developer Tools 来查看隐藏标记。标记不应该到客户端。

回答by Tomas McGuinness

The syntax for something like that would be

类似的语法是

<% if(System.Configuration.ConfigurationManager.AppSettings["IsDemo"] == "true") %>
<% { %>
<!-- Protected HTML goes here -->
<% } %>

This assumes that the page is in C#.

这假定页面是在 C# 中的。

You can firm this code up by being more defensive around the AppSettings retrieval e.g. what happens in the case where the value is null etc.

您可以通过对 AppSettings 检索更具防御性来巩固此代码,例如在值为 null 的情况下会发生什么等。

回答by Parag Meshram

Solution:-

解决方案:-

<% If (ConfigurationManager.AppSettings("IsDemo").ToLower().Equals("true")) Then%>
    <tr>
       <.....>
    </tr>
<% Else%>
    <tr>
        <.....>
    </tr>
<% End If%>

回答by TheVillageIdiot

If I understand it right, you don't want to use server-side (aspx components, with runat="server" attribute) and just want to control display of html on aspx page then try this solution.

如果我理解正确,您不想使用服务器端(aspx 组件,带有 runat="server" 属性),只想控制 html 在 aspx 页面上的显示,然后尝试此解决方案。

Create a property in codebehind file (or better still in some other config helper class):

在代码隐藏文件中创建一个属性(或者更好的是在其他一些配置助手类中):

//IN C# (OR VB) file
protected string Demo{
    get{ 
            return ConfigurationManager.AppSettings["IsDemo"]=="true"?
                   "none":"block";
      }
}

In aspx page:

在aspx页面中:

<tr style="display:<%= Demo%>;">
    <td>blah blah</td>
</tr>