=x= 🌵 COMPANY - Certificat 前台頁面後端功能製作。
📌 Certificat 頁面內容分為 : 文字區、直式並排圖片區、橫式並排圖片區。
<head>
及 <body>
內的內容分別複製到 .aspx 檔案中。protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
loadContentText();
loadContentImgV();
loadContentImgH();
}
}
private void loadContentText()
{
//從資料庫取內文資料
SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["TayanaYachtConnectionString"].ConnectionString);
string sqlCountry = "SELECT TOP 1 certificatContent FROM Company";
SqlCommand command = new SqlCommand(sqlCountry, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read()) {
//渲染畫面
ContentText.Text = reader["certificatContent"].ToString();
}
connection.Close();
}
private void loadContentImgV()
{
//會重複添加內容所以用 StringBuilder 效能較好
StringBuilder ImgVHtml = new StringBuilder();
//用 List<T> 來存取 JSON 格式圖片檔名
List<ImagePathV> savePathListV = new List<ImagePathV>();
//從資料庫取出直式圖檔檔名
SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["TayanaYachtConnectionString"].ConnectionString);
string sqlLoad = "SELECT TOP 1 certificatVerticalImgJSON FROM Company";
SqlCommand command = new SqlCommand(sqlLoad, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read()) {
string loadJson = HttpUtility.HtmlDecode(reader["certificatVerticalImgJSON"].ToString());
//反序列化JSON格式
savePathListV = JsonConvert.DeserializeObject<List<ImagePathV>>(loadJson);
}
connection.Close();
// ?. 可用來判斷不是 Null 才執行 Count
if (savePathListV?.Count > 0) {
foreach (var item in savePathListV) {
ImgVHtml.Append($"<li><p><img src='images/{item.SaveName}' alt='Tayana ' /></p></li>");
}
}
//渲染畫面
ContentImgV.Text = ImgVHtml.ToString();
}
// JSON 資料 V 直式
public class ImagePathV
{
public string SaveName { get; set; }
}
private void loadContentImgH()
{
//會重複添加內容所以用 StringBuilder 效能較好
StringBuilder ImgHHtml = new StringBuilder();
//用 List<T> 來存取 JSON 格式圖片檔名
List<ImagePathH> savePathListH = new List<ImagePathH>();
//從資料庫取出橫式圖檔檔名
SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["TayanaYachtConnectionString"].ConnectionString);
string sqlLoad = "SELECT TOP 1 certificatHorizontalImgJSON FROM Company";
SqlCommand command = new SqlCommand(sqlLoad, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read()) {
string loadJson = HttpUtility.HtmlDecode(reader["certificatHorizontalImgJSON"].ToString());
//反序列化JSON格式
savePathListH = JsonConvert.DeserializeObject<List<ImagePathH>>(loadJson);
}
connection.Close();
// ?. 可用來判斷不是 Null 才執行 Count
if (savePathListH?.Count > 0) {
foreach (var item in savePathListH) {
ImgHHtml.Append($"<li><p><img src='images/{item.SaveName}' alt='Tayana ' width='319px' height='234px' /></p></li>");
}
}
ContentImgH.Text = ImgHHtml.ToString();
}
// JSON 資料 H 直式
public class ImagePathH
{
public string SaveName { get; set; }
}
📢 昨天跟今天的 COMPANY 頁面,因為有兩個獨立的內容,所以可以完整的練習到,需要拆解分類的畫面,以及只要一口氣送出全部的畫面,認真覺得如果沒有先練習這些,很難直接去挑戰 NEWS 頁跟 Yachts 頁的內容,建議大家要真的搞懂 COMPANY 頁包含後台實作的內容,對後續頁面的製作來說非常實用。