"""Offline ASP.NET WebForms verifier for Chapter 45 (databinding)."""
from __future__ import annotations
import html
import json
VERSION = "CPython 3.12.14 offline ASP.NET WebForms verifier (IIS/ASP.NET runtime absent)"
def render_list_control(control_type: str, ctrl_id: str, items: list[tuple[str, str]]) -> str:
"""Render WebForms data-bound list controls to minimal representative HTML."""
ctype = control_type.lower()
if ctype == "radiobuttonlist":
rows = "".join(
f'<tr><td><input id="{ctrl_id}_{i}" type="radio" name="{ctrl_id}" value="{html.escape(val, quote=True)}" />'
f'<label for="{ctrl_id}_{i}">{html.escape(txt)}</label></td></tr>'
for i, (val, txt) in enumerate(items)
)
return f'<table id="{ctrl_id}">{rows}</table>'
if ctype == "checkboxlist":
rows = "".join(
f'<tr><td><input id="{ctrl_id}_{i}" type="checkbox" name="{ctrl_id}${i}" value="{html.escape(val, quote=True)}" />'
f'<label for="{ctrl_id}_{i}">{html.escape(txt)}</label></td></tr>'
for i, (val, txt) in enumerate(items)
)
return f'<table id="{ctrl_id}">{rows}</table>'
if ctype == "dropdownlist":
opts = "".join(
f'<option value="{html.escape(val, quote=True)}">{html.escape(txt)}</option>'
for val, txt in items
)
return f'<select name="{ctrl_id}" id="{ctrl_id}">{opts}</select>'
if ctype == "listbox":
opts = "".join(
f'<option value="{html.escape(val, quote=True)}">{html.escape(txt)}</option>'
for val, txt in items
)
return f'<select size="4" name="{ctrl_id}" id="{ctrl_id}">{opts}</select>'
raise ValueError(f"Unsupported list control: {control_type}")
def main() -> None:
static_items = [("N", "Norway"), ("S", "Sweden"), ("F", "France"), ("I", "Italy")]
radio_html = render_list_control("RadioButtonList", "countrylist", static_items)
# Case 2: All 4 supported list controls bound to the same data source
controls = ["RadioButtonList", "CheckBoxList", "DropDownList", "ListBox"]
bound_tags = {
c: render_list_control(c, "ctrl", static_items[:2])
for c in controls
}
# Case 3: Updating external data source updates rendered options without touching markup
updated_source = static_items + [("D", "Denmark")]
updated_dropdown = render_list_control("DropDownList", "countryDrop", updated_source)
# Case 4: IsPostBack guard preserves selected value vs unguarded rebind resets selection
def simulate_postback_bind(guard_postback: bool, user_selected: str) -> str:
selected = user_selected
if not guard_postback:
# Re-binding on postback resets selection to first item
selected = static_items[0][0]
return selected
guarded_sel = simulate_postback_bind(guard_postback=True, user_selected="F")
unguarded_sel = simulate_postback_bind(guard_postback=False, user_selected="F")
cases = [
{
"name": "static_radiobuttonlist_items",
"summary": "解析静态声明的 asp:RadioButtonList 及 4 个 asp:ListItem 并核对渲染输出",
"expected": {
"item_count": 4,
"contains_norway": True,
"contains_italy": True,
},
"actual": {
"item_count": radio_html.count('type="radio"'),
"contains_norway": 'value="N"' in radio_html and ">Norway<" in radio_html,
"contains_italy": 'value="I"' in radio_html and ">Italy<" in radio_html,
},
"passed": (
radio_html.count('type="radio"') == 4
and 'value="N"' in radio_html
and ">Norway<" in radio_html
and 'value="I"' in radio_html
and ">Italy<" in radio_html
),
},
{
"name": "four_list_controls_binding",
"summary": "核对 RadioButtonList、CheckBoxList、DropDownList、ListBox 四类列表控件的数据绑定渲染形态",
"expected": {
"RadioButtonList": 'type="radio"',
"CheckBoxList": 'type="checkbox"',
"DropDownList": "<select name=\"ctrl\"",
"ListBox": '<select size="4"',
},
"actual": {
"RadioButtonList": 'type="radio"' if 'type="radio"' in bound_tags["RadioButtonList"] else "",
"CheckBoxList": 'type="checkbox"' if 'type="checkbox"' in bound_tags["CheckBoxList"] else "",
"DropDownList": '<select name="ctrl"' if '<select name="ctrl"' in bound_tags["DropDownList"] else "",
"ListBox": '<select size="4"' if '<select size="4"' in bound_tags["ListBox"] else "",
},
"passed": (
'type="radio"' in bound_tags["RadioButtonList"]
and 'type="checkbox"' in bound_tags["CheckBoxList"]
and '<select name="ctrl"' in bound_tags["DropDownList"]
and '<select size="4"' in bound_tags["ListBox"]
),
},
{
"name": "datasource_separation_update",
"summary": "验证数据与 HTML 分离后,在外部数据源追加 Denmark 无需修改页面标记即可同步呈现 5 项",
"expected": {"option_count": 5, "has_denmark": True},
"actual": {
"option_count": updated_dropdown.count("<option "),
"has_denmark": '<option value="D">Denmark</option>' in updated_dropdown,
},
"passed": (
updated_dropdown.count("<option ") == 5
and '<option value="D">Denmark</option>' in updated_dropdown
),
},
{
"name": "databind_lifecycle_idempotency",
"summary": "对比首次加载绑定(IsPostBack 保护)保留用户选中项 F 与未保护重绑重置为首项 N 的差异",
"expected": {"guarded_selected": "F", "unguarded_selected": "N"},
"actual": {"guarded_selected": guarded_sel, "unguarded_selected": unguarded_sel},
"passed": guarded_sel == "F" and unguarded_sel == "N",
},
]
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/web-pages-aspnet-databinding-lab/explore-aspnet-databinding.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-aspnet-databinding.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a3037.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3020.zip