ConfigurationManager.AppSettings在<title>中工作,但在<asp:Label>中不工作

时间:2019-11-20 10:01:33

标签: asp.net

我从web.config获得了<title><asp:Label>的价值。
对于<title>,它由ConfigurationManager.AppSettings["Key"]工作。
对于<asp:Label>,它由AppSettings: Key工作。
是否可以管理代码并使用ConfigurationManager.AppSettings["Key"]AppSettings: Key
例如:仅使用ConfigurationManager.AppSettings["Key"]

web.config

<configuration>
  <appSettings>
    <add key="Title" value="My Title"/>
  </appSettings>
</configuration>

.aspx

<head runat="server">
    <%--It's working--%>
    <title><%: ConfigurationManager.AppSettings["Title"] %></title>

    <%--It's not working--%>
    <%--Compiler Error Message: CS0103: The name 'Title' does not exist in the current context--%>
    <%--<title><%: AppSettings: Title %></title>--%>
</head>
<body>
    <%--It's not working--%>
    <%--Parser Error Message: Server tags cannot contain <% ... %> constructs.--%>
    <%--<asp:Label ID="label1" Text="<%: ConfigurationManager.AppSettings["Title"] %>" runat="server" />--%>

    <%--It's working--%>
    <asp:Label ID="label1" Text="<%$ AppSettings: Title %>" runat="server" />
</body>

输出 ConfigurationManager.AppSettings is working in <title> but not for <asp:Label>

1 个答案:

答案 0 :(得分:1)

    <appSettings>
        <add key ="Name" value ="Name"/>
    </appSettings>

     <asp:Label ID="lbl" runat="server" Text="<%$appSettings:Name %>"></asp:Label>

     <asp:TextBox ID = "txtBox1" runat = "server" Text = "<%$appSettings:Name %>" />

     <span><%=System.Configuration.ConfigurationManager.AppSettings["Name"]%></span>

这是实现...的最简单方法