備忘錄_20160105(定位) 修改 回首頁

程式 2017-08-07 11:16:48 1502075808 100
asp.net 程式參考 ^.^ (session, sqlite, post data, custom function)

asp.net 程式參考 ^.^ (session, sqlite, post data, custom function)

自己開發的元件 dll 請放到根目錄下 bin 資料夾內 /wwwroot/bin/projDll4WebPages_20170807.dll

SQLite 相關元件也是放到根目錄下 bin 資料夾內 /wwwroot/bin/

 asp.net 2.0 (64bit) SQLite.Designer.dll, SQLite.Interop.dll, System.Data.SQLite.dll

 asp.net 4.0 (32bit) SQLite.Designer.dll, System.Data.SQLite.dll, System.Data.SQLite.EF6.dll, System.Data.SQLite.Linq.dll

主要網頁 /wwwroot/Default.aspx

次要網頁 /wwwroot/getPost.aspx

組態設定,設定成看到詳細錯誤訊息,方便開發。 /wwwroot/Web.config

若資料夾有權限問題而無法寫入,可以在那個資料夾的安全性設定,設定 IIS_IUSRS 有寫入、修改的權限。


CMath.cs 原始碼,請編譯後,把 projDll4WebPages_20170807.dll 放到 wwwroot/bin 資料夾底下。

using System;
using System.Collections.Generic;
using System.Text;

namespace projDll4WebPages_20170807
{
    public class CMath
    {
        public int iAdd(int i1, int i2)
        {
            return i1 + i2;
        }
    }
}

Default.aspx 原始碼

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="projDll4WebPages_20170807" %><!-- using projDll4WebPages_20170807; -->
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Data.SQLite" %>
<!DOCTYPE html>
<html>
  <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>用記事本開發 ASP.NET 參考範本</title>
  </head>
  <body>
    用記事本開發 ASP.NET 參考範本~~~<br />
    若有用 asp:Button 的話,form runat="server" 的標籤就必須存在喔<br />
    <form id="form1" runat="server">
      <div>
        
        <asp:Button ID="btnDllTest" runat="server" OnClick="btnDllTest_Click" Text="我會呼叫 dll 的函數,並放入文字盒中。" /><br />
        <asp:TextBox ID="tbDllTest" runat="server"></asp:TextBox><br />
        
        <% lblRightNow.Text = "中文載入~123,要放在標記 asp:Label 之前喔!"; %>
        <asp:Label ID="lblRightNow" runat="server"></asp:Label><br />
        
        <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>

        <asp:Label ID="lblSQLite" runat="server"></asp:Label><br />
        
      </div>
    </form>
    <form method="post" action="getPost.aspx">
      請輸入參數<input type="text" id="txtPost1" name="txtPost1" value=""><br />
      <input type="submit" value="送出參數"><br />
    </form>
    <script runat="server">
      
      projDll4WebPages_20170807.CMath oMath=new projDll4WebPages_20170807.CMath();
      static SQLiteConnection m_dbConn;
      
      protected void Page_Load(object sender, EventArgs e)
      {
        Session["session20170807"]="Session set!!!";
        Response.Write("Page_Load here......");
        runSQLite();
      }
      
      protected void runSQLite()
      {
        SQLiteCommand oCmd;
        string strPath=Server.MapPath("~/data/20170808.sqlite");
        
        //SQLiteConnection.CreateFile(strPath);
        
        m_dbConn=new SQLiteConnection("Data Source="+strPath+";Version=3");
        m_dbConn.Open();
        
        string strSQL="";
        
        /*
        strSQL="create table tb1(i int,vc varchar(64) not null)";
        oCmd=new SQLiteCommand(strSQL,m_dbConn);
        oCmd.ExecuteNonQuery();
        
        strSQL="insert into tb1(i,vc) values(1,'Good')";
        oCmd=new SQLiteCommand(strSQL,m_dbConn);
        oCmd.ExecuteNonQuery();
        
        strSQL="insert into tb1(i,vc) values(2,'許蓋功')";
        oCmd=new SQLiteCommand(strSQL,m_dbConn);
        oCmd.ExecuteNonQuery();
        */
        
        Response.Write("<br />");
        strSQL="select * from tb1";
        oCmd=new SQLiteCommand(strSQL,m_dbConn);
        SQLiteDataReader oRdr=oCmd.ExecuteReader();
        while(oRdr.Read())
        {
          Response.Write("i:"+oRdr["i"]+"  vc:"+oRdr["vc"]+"<br />");
        }
        oRdr.Close();
        
        m_dbConn.Close();
      }
      
      protected void btnDllTest_Click(object sender, EventArgs e)
      {
        int iAns=oMath.iAdd(38,83);
        tbDllTest.Text = "The function works!! Answer for 38+83 is "+iAns.ToString();
      }
      
      protected void Calendar1_SelectionChanged(object sender, EventArgs e)
      {
        Response.Write(Calendar1.SelectedDate.ToShortDateString());
      }
      
    </script>
  </body>
</html>

getPost.aspx 原始碼

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Web" %>
<!DOCTYPE html>
<html>
  <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>用記事本開發 ASP.NET 參考範本2</title>
  </head>
  <body>
    用記事本開發 ASP.NET 參考範本2~~~<br />
    <div>
      <asp:Label ID="lbl001" runat="server"></asp:Label><br />
      <asp:Label ID="lbl002" runat="server"></asp:Label><br />
      <asp:Label ID="lbl003" runat="server"></asp:Label><br />
    </div>
    <script runat="server">
      
      protected void Page_Load(object sender, EventArgs e)
      {
        Response.Write("Page_Load here......<br />");
        lbl001.Text = Request.Form["txtPost1"];
        lbl002.Text = Request.Form["txtNullVariable"]; // 不會有錯誤訊息
        lbl003.Text = Session["session20170807"].ToString();
      }
      
    </script>
  </body>
</html>

Web.config 原始碼

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <appSettings/>
    <connectionStrings/>
    <system.web>
        <compilation debug="true">
        </compilation>
        <!--
            <authentication> 區段可讓您設定 
            ASP.NET 用來識別連入使用者的 
            安全性驗證模式。 
        -->
        <authentication mode="Windows" />
        <!--
            <customErrors> 區段可讓您設定 
            當執行要求期間發生無法處理 
            之錯誤時的處理方式。更明確地說,
            開發人員可以利用它設定要顯示的  
            html 錯誤頁面,以取代錯誤堆疊追蹤。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm"/>
            <error statusCode="404" redirect="FileNotFound.htm"/>
        </customErrors>
        -->
        <customErrors mode="Off"/>
    </system.web>
</configuration>