[.NET Webform]_ListView_ul li項目清單列表綁定

 
之前介紹過一篇是針對table的資料樣式綁定

而這次要來透過ListVIew綁定的對應html tag則是 ul li的項目清單列表



aspx

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample1_ul_li.aspx.cs" Inherits="Sample1_ul_li" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ListView ID="lsv_consortable" runat="server">
                <LayoutTemplate>
                    <ul id="consortable" runat="server" class="sortitem">
                        <li id="itemPlaceHolder" runat="server" />
                    </ul>
                </LayoutTemplate>
                <ItemTemplate>
                    <li id="<%# Eval("Country")%>"><%# Eval("Country")%></li>
                </ItemTemplate>
            </asp:ListView>
            <%--<ul id="consortable" class="sortitem">
                <li id="TW">TW</li>
                <li id="GB">GB</li>
                <li id="KR">KR</li>
                <li id="JP">JP</li>
                <li id="WO">WO</li>
                <li id="EP">EP</li>
                <li id="CA">CA</li>
                <li id="AU">AU</li>
                <li id="US">US</li>
                <li id="CN">CN</li>
            </ul>--%>
        </div>
    </form>
</body>
</html>


aspx.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Sample1_ul_li : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        string strSQL = " select distinct Country from Customers";
        DataTable dtCustomers = GetDataTable(strConn, strSQL);
        lsv_consortable.DataSource = dtCustomers;
        lsv_consortable.DataBind();
    }

    private DataTable GetDataTable(string strConn, string strSQL, int shiftDataSet = -1)
    {
        try
        {
            using (var conn = new SqlConnection())
            {
                conn.ConnectionString = strConn;
                conn.Open();
                using (var SqlCmd = new SqlCommand())
                {
                    SqlCmd.Connection = conn;
                    SqlCmd.CommandText = strSQL;
                    var dt = new DataTable();
                    int idxDs = 0;
                    shiftDataSet = shiftDataSet - 1;
                    using (var dr = SqlCmd.ExecuteReader())
                    {
                        var loopTo = shiftDataSet;
                        for (idxDs = 0; idxDs <= loopTo; idxDs++)
                            dr.NextResult();
                        dt.Load(dr);
                    }

                    return dt;
                    dt = null;
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}






留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題