測試文字功能,小工具,標題

記錄很重要,不然會浪費很多時間在找以前的記憶

一個人的氣度,決定他未來的高度。

2016年1月25日 星期一

ASP.NET Inline code 前置程式碼 寫法 含 引用

ASP.NET Inline code 前置程式碼 寫法 含 引用

=======================Default.aspx==============================

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<!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 runat="server">

 protected void Page_Load(object sender, EventArgs e)
    {
        string tClientIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
        string Dir = Page.Server.MapPath("~/");
        StreamWriter sw = File.AppendText(Dir + "LOG.txt");
        sw.WriteLine(tClientIP + " " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
        sw.Flush();
        sw.Close();
    }

</script>

</head>

<FRAMESET >
  <FRAME src="welcome.htm"/>
</FRAMESET>

</html>


=======================Default2.aspx==============================
<%@ Page Language="C#" %>

<%@ Import Namespace="System.IO" %>

<!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 id="Head1" runat="server">
    <title></title>
<script runat="server">

    string Dir =string.Empty ;
    string str = "";

    protected void Page_Load(object sender, EventArgs e)
    {  
        Dir =  Page.Server.MapPath("~/");
        using (StreamReader sr = new StreamReader(Dir + "LOG.txt")) //路徑
        {
            while ((str = sr.ReadLine()) != null)
            {
                Response.Write(sr.ReadLine() + "<br />"); //讀取單行
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        using (StreamWriter sw = new StreamWriter(Dir + "LOG.txt")) //路徑
        {
            sw.Flush();
            sw.Close();
        }
        Response.Redirect(Request.Url.ToString());
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="清除記錄" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

沒有留言:

張貼留言