"""Offline ASP.NET WebForms verifier for Chapter 51 (datalist)."""
from __future__ import annotations
import html
import json
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline ASP.NET WebForms verifier (IIS/ASP.NET runtime absent)"
CDCATALOG_XML = """<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><price>10.90</price></cd>
<cd><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price></cd>
<cd><title>Greatest Hits</title><artist>Dolly Parton</artist><price>9.90</price></cd>
<cd><title>Still got the blues</title><artist>Gary Moore</artist><price>10.20</price></cd>
<cd><title>Eros</title><artist>Eros Ramazzotti</artist><price>9.90</price></cd>
</catalog>
"""
def load_cds(xml_text: str) -> list[dict[str, str]]:
root = ET.fromstring(xml_text)
return [
{c.tag: (c.text or "").strip() for c in cd}
for cd in root.findall("cd")
]
def render_datalist(
ctrl_id: str,
rows: list[dict[str, str]],
*,
gridlines: str | None = None,
table_attrs: dict[str, str] | None = None,
header_style: str = "",
item_style: str = "",
alt_item_style: str = "",
footer_style: str = "",
header_text: str = "My CD Catalog",
footer_text: str = "Copyright Demo",
) -> str:
"""DataList automatically wraps header, items, and footer inside an HTML <table>."""
attrs = [f'id="{html.escape(ctrl_id, quote=True)}"']
if gridlines == "both":
attrs.extend(['rules="all"', 'border="1"'])
for k, v in (table_attrs or {}).items():
attrs.append(f'{k}="{html.escape(v, quote=True)}"')
def td(content: str, style: str) -> str:
style_attr = f' style="{style}"' if style else ""
return f"<tr><td{style_attr}>{content}</td></tr>"
parts = [f"<table {' '.join(attrs)}>"]
parts.append(td(html.escape(header_text), header_style))
for idx, row in enumerate(rows):
line = f'"{html.escape(row["title"])}" of {html.escape(row["artist"])} - ${html.escape(row["price"])}'
style = alt_item_style if (alt_item_style and idx % 2 == 1) else item_style
parts.append(td(line, style))
parts.append(td(html.escape(footer_text), footer_style))
parts.append("</table>")
return "".join(parts)
def main() -> None:
rows = load_cds(CDCATALOG_XML)
# Case 1: Auto-table wrapping even without user <table> tags
auto_table_html = render_datalist("cdcatalog", rows)
# Case 2: gridlines="both"
grid_html = render_datalist("cdcatalog", rows, gridlines="both")
# Case 3: Styled DataList (headerstyle, itemstyle, footerstyle)
styled_html = render_datalist(
"cdcatalog",
rows,
table_attrs={
"cellpadding": "2",
"cellspacing": "2",
"style": "background-color:#e8e8e8;border-style:inset;width:100%;",
},
header_style="font-family:Verdana;font-size:12pt;text-align:center;font-weight:bold;",
item_style="background-color:#778899;color:#ffffff;",
footer_style="font-size:9pt;font-style:italic;",
)
# Case 4: AlternatingItemTemplate + alternatingitemstyle
alt_html = render_datalist(
"cdcatalog",
rows,
table_attrs={
"cellpadding": "2",
"cellspacing": "2",
"style": "background-color:#e8e8e8;border-style:inset;width:100%;",
},
header_style="font-family:Verdana;font-size:12pt;text-align:center;font-weight:bold;",
item_style="background-color:#778899;color:#ffffff;",
alt_item_style="background-color:#e8e8e8;color:#000000;",
footer_style="font-size:9pt;font-style:italic;",
)
cases = [
{
"name": "datalist_xml_load_and_auto_table",
"summary": "验证 DataList 无需手写 <table> 即自动为表头、5 条数据项与页脚生成 7 行表格布局",
"expected": {"row_count": 5, "has_auto_table": True, "tr_count": 7},
"actual": {
"row_count": len(rows),
"has_auto_table": auto_table_html.startswith('<table id="cdcatalog">'),
"tr_count": auto_table_html.count("<tr>"),
},
"passed": (
len(rows) == 5
and auto_table_html.startswith('<table id="cdcatalog">')
and auto_table_html.count("<tr>") == 7
),
},
{
"name": "datalist_gridlines_basic_render",
"summary": "验证设置 gridlines='both' 后输出 rules='all' border='1' 并正确格式化唱片标题与价格",
"expected": {
"has_grid_border": True,
"first_item_rendered": True,
},
"actual": {
"has_grid_border": 'rules="all" border="1"' in grid_html,
"first_item_rendered": '"Empire Burlesque" of Bob Dylan - $10.90' in grid_html,
},
"passed": (
'rules="all" border="1"' in grid_html
and '"Empire Burlesque" of Bob Dylan - $10.90' in grid_html
),
},
{
"name": "datalist_styled_templates_render",
"summary": "验证 DataList 表格级样式与 headerstyle/itemstyle/footerstyle 属性映射至内联 CSS",
"expected": {
"has_border_inset": True,
"header_styled_count": 1,
"item_styled_count": 5,
"footer_italic_count": 1,
},
"actual": {
"has_border_inset": "border-style:inset" in styled_html,
"header_styled_count": styled_html.count("font-family:Verdana;font-size:12pt"),
"item_styled_count": styled_html.count("background-color:#778899;color:#ffffff;"),
"footer_italic_count": styled_html.count("font-size:9pt;font-style:italic;"),
},
"passed": (
"border-style:inset" in styled_html
and styled_html.count("font-family:Verdana;font-size:12pt") == 1
and styled_html.count("background-color:#778899;color:#ffffff;") == 5
and styled_html.count("font-size:9pt;font-style:italic;") == 1
),
},
{
"name": "datalist_alternating_styled_render",
"summary": "验证加入 AlternatingItemTemplate 后 5 条数据项呈 3 条深蓝灰底白字 + 2 条浅灰底黑字交替展示",
"expected": {"primary_items": 3, "alternating_items": 2},
"actual": {
"primary_items": alt_html.count("background-color:#778899;color:#ffffff;"),
"alternating_items": alt_html.count("background-color:#e8e8e8;color:#000000;"),
},
"passed": (
alt_html.count("background-color:#778899;color:#ffffff;") == 3
and alt_html.count("background-color:#e8e8e8;color:#000000;") == 2
),
},
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。
入口文件:projects/mvc-aspnet-datalist-lab/explore-aspnet-datalist.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-aspnet-datalist.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2854.zip
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2870.zip