本篇重點:
- .aspx / .aspx.vb 介紹
- Hello World
- 變數處理
.aspx / .aspx.vb 介紹
建立好 Web Form 後,如果是選擇 vb 語法裡面會包含.aspx 檔以及 .aspx.vb 檔。
.aspx:可以解釋成網頁的前台,透過 HTML 和可拉式控制項來布局和調整網頁外觀。 .aspx 檔案中所有控制項都可以在對應的 .aspx.vb 中使用。
.aspx.vb:網頁的後台,與對應的.aspx 關聯的程式碼檔案,負責處理伺服器端的事件和邏輯。此檔案中程式碼可以與 .aspx 檔案中的控制項互動,實現動態網頁的功能。
總結來說,.aspx 檔案用於定義網頁外觀,而.aspx.vb 檔案則是處理背後的事件和邏輯。這兩個檔案一起工作,使 ASP.NET Web 應用程式能夠具備動態和互動的功能。
Hello World
接著帶大家認識一下 .aspx 及 .aspx.vb 編輯的地方~~~~~
在頁面中直接點表單 (Test1.aspx),把程式碼改成這串:
<div> hello world! </div>
接著點中間上方綠色箭頭的編譯按鈕,就可以看見剛剛打的字出現在網頁上
這就是透過.aspx 檔案輸出內容的方式。一般會在這裡拉完控制項之後,進到.aspx.vb頁面去寫程式碼。
接下來在 Test1.aspx 點滑鼠右鍵打開檢視程式碼,就會看到後端的編輯頁面了。
變數處理
以下會用 vb 語法來說明幾種基本的變數處理:
- 變數宣告:可以使用 Dim 來宣告字串變數
Dim str As String = "Hello World"
- 字串連接:使用 & 運算符號將兩個字串連接在一起
Dim str1 As String = "Hello"
Dim str2 As String = "World"
Dim result As String = str1 & " " & str2
'結果為 "Hello World"
- 字串長度:使用 Len 函數來獲取字串的長度
Dim str As String = "Hello World"
Dim length As Integer = Len(str)
'結果為 11
- 字串切割:使用Split函數將字串拆分為字串陣列
Dim str As String = "apple,banana,orange"
Dim fruits() As String = str.Split(",")
'fruits 為字串陣列 ["apple", "banana", "orange"]
- 字串取子字串:使用 Substring 從字串中提取子字串 () 代表從第幾位元後的字串
Dim str As String = "Hello World"
Dim subStr As String = str.Substring(6)
'結果為 "World"
- 字串替換:使用 Replace 將字串中的特定字串替換為新字串
Dim str As String = "Hello World"
Dim newStr As String = str.Replace("World", "Universe")
'結果為 "Hello Universe"
- 大小寫轉換:使用 ToUpper 和 ToLower 將字串轉換為大寫或小寫
Dim str As String = "Hello World"
Dim upperStr As String = str.ToUpper() '結果為 "HELLO WORLD"
Dim lowerStr As String = str.ToLower() '結果為 "hello world"
- 字串轉換:使用Int、 CInt 等函數將字串轉換為數值型別
Dim num As Double = 10.67
Dim result1 As Integer = Int(num) '會將數值捨去小數取整數,結果為10
Dim result2 As Integer = CInt(num) '會將數值四捨五入取整數,結果為11
- 字串補0:使用 String.PadLeft 在字串的左側補零
Dim num1 As Integer = 7
Dim num2 As String = num1.ToString().PadLeft(3, "0")
'將7轉換為字串,並在左側補0,結果為 "007"
