假設我目前的 web.config 設定了 TestEmpNo 如下
<appSettings> <add key="TestEmpNo" value="526275" /> </appSettings>
在 .aspx 頁面裡,我的 javascript 就可以寫成如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConTest.aspx.cs" Inherits="ConTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        alert('Admin Empno:' + '<asp:Literal runat="server" Text="<%$ appSettings:TestEmpNo%>" />');
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
這裡是搭配以前所提到「SqlDataSource 使用 web.config 的 appSettings」的文章,透過<%$ appSettings:TestEmpNo%> 去抓取 Web.config 內容。
如果你有很多的 javascript 都有共用資訊的需求時,統一放在 web.config 裡也不失為一個好方法。但前提必須是沒有機密性的資料才行,不然你只要檢視網頁原始碼,就....就被看光光了啦~
參考:
01:HOW TO:將 Literal Web 伺服器控制項加入至 Web Form 網頁
02:ASP:Literal控件用法
03:SqlDataSource 使用 web.config 的 appSettings
沒有留言:
張貼留言